Unimatrix_001
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
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;
}
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!).
ASKER
Hm, I've seen that typeid thing, but I thought that was run-time, not compile-time as templates are?
ASKER
>>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
So I would need something like:
struct NoArg {virutal void rttisupplier(){}};
?
Thanks,
Uni
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> 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 :)
Yes, runtime -- I was covering all bases for you :)
ASKER
Thank you! I'll have a good look at this... looks interesting... :)
ASKER
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
Thanks, :)
Uni
ASKER
Actually, no I'll put that in a new question...
Out right now but I'll see what I can do when I return.
ASKER
Hi Ricky,
New question:
https://www.experts-exchange.com/questions/24563543/Follow-up-Finding-template-parameter-types.html
:)
New question:
https://www.experts-exchange.com/questions/24563543/Follow-up-Finding-template-parameter-types.html
:)
ASKER
No problem mate. :)
http://www.cplusplus.com/r
if(typeid(A) == typeid(NoArg))
{
same thing
}