Solved

Many templates for a class.

Posted on 2009-07-11
4
189 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
  • 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now