Link to home
Start Free TrialLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Finding template parameter types.

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

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

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

if(typeid(A) == typeid(NoArg))
{
   same thing
}
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!).
Avatar of Unimatrix_001

ASKER

Hm, I've seen that typeid thing, but I thought that was run-time, not compile-time as templates are?
>>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
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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 :)
Thank you! I'll have a good look at this... looks interesting... :)
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
Actually, no I'll put that in a new question...
Out right now but I'll see what I can do when I return.
No problem mate. :)