Unimatrix_001
asked on
Many templates for a class.
Hi,
Is there anyway to achieve something as in the following code, where vars 2 and 3 are only used if that specific template is used?
Thanks,
Uni.
Is there anyway to achieve something as in the following code, where vars 2 and 3 are only used if that specific template is used?
Thanks,
Uni.
template<typename A>
template<typename A, typename B>
template<typename A, typename B, typename C>
class CMyClass{
A var1;
B var2;
C var3;
};
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A work around is to use a "NoArg" flag that is used as the default parameter. The flag is just an empty struct. Of course, if you then write code that instantiates a concrete template that tries to use the NoArg flag in anyway that is erroneous you'll get a compile time error. This is the solution Boost often uses to simulate variable argument templates.
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;
}
ASKER
Hm... I think I'll just wait for C++0x and the variadic templates (I think that's the correct name...). ;)
Thanks,
Uni
Thanks,
Uni
ASKER
Thank you.
Uni