Solved

Finding template parameter types.

Posted on 2009-07-12
12
234 Views
Last Modified: 2012-05-07
Hello.

Evilrix mentioned in a previous question that the Boost library use something as in the code below to simulate variadic templates. What I'm wondering though is how can I check if a template parameter is equal to NoArg and create the internals of the template appropriately? I don't seem to be able to use partial template specialisation as when I do I get errors that default parameters are not allowed with their use. So, how do we go about this, so that something like CMyClass<int> would generate:

CMyClass{
      void method(int);
}

and CMyClass(int, int) would generate:

CMyClass{
      void method(int, int);
}

Thank you,
Uni
struct NoArg {};
 
template<typename A, typename B = NoArg, typename C = NoArg>
class CMyClass{
       A var1;
       B var2;
       C var3;
};
 
int main()
{
        CMyClass<int> ci;
        CMyClass<int, float> cif;
        CMyClass<int, float, long> cifl;
}

Open in new window

0
Comment
Question by:Unimatrix_001
  • 7
  • 5
12 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24834573
Well, this is one way..
http://www.cplusplus.com/reference/std/typeinfo/type_info/

if(typeid(A) == typeid(NoArg))
{
   same thing
}
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24834580
Note, that relies on the type have RTTI info, so it has to have at least one virtual function (oops, forgot to point that out!).
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834581
Hm, I've seen that typeid thing, but I thought that was run-time, not compile-time as templates are?
0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834588
>>so it has to have at least one virtual function (oops, forgot to point that out!).
So I would need something like:

struct NoArg {virutal void rttisupplier(){}};

?

Thanks,
Uni
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 24834597
Or the template way -- although this is resolved at compile time and not runtime.
#include <iostream>
 
struct NoArg {};
 
template <typename T>
struct IsNoArg
{
	enum { YES = 0 };
};
 
template <>
struct IsNoArg<NoArg>
{
	enum { YES = 1 };
};
 
template<typename A >
class CMyClass{
public:
	void foo()
	{
		if(IsNoArg<A>::YES)
		{
			std::cout << "Yes" << std::endl;
		}
		else
		{
			std::cout << "No" << std::endl;
		}
	}
};
 
 
int main()
{
	CMyClass<int> c1;
	c1.foo();
 
	CMyClass<NoArg> c2;
	c2.foo();
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 24834598
>> Hm, I've seen that typeid thing, but I thought that was run-time, not compile-time as templates are?
Yes, runtime -- I was covering all bases for you :)
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31602580
Thank you! I'll have a good look at this... looks interesting... :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834632
Hm, just a follow-up... How could this be used to create differing function declarations though as that "if(IsNoArg<A>::YES)" although is runtime, occurs within the function foo(). See very top of question for further info...

Thanks, :)
Uni
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834707
Actually, no I'll put that in a new question...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24834722
Out right now but I'll see what I can do when I return.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834723
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834726
No problem mate. :)
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question