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

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.
template<typename A>
template<typename A, typename B>
template<typename A, typename B, typename C>
class CMyClass{
       A var1;
       B var2;
       C var3;
};

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany 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
Avatar of Unimatrix_001

ASKER

Ah, I was afraid of such. :(

Thank you.
Uni
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;
}

Open in new window

Hm... I think I'll just wait for C++0x and the variadic templates (I think that's the correct name...). ;)

Thanks,
Uni