Solved

Many templates for a class.

Posted on 2009-07-11
4
220 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:Unimatrix_001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 24831228
No, this won't work that way, sorry. There's the possibility to use default parameters, but no way to have them default to 'not present'.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24831236
Ah, I was afraid of such. :(

Thank you.
Uni
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24833854
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

0
 
LVL 3

Author Comment

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

Thanks,
Uni
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

726 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