Solved

Many templates for a class.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

822 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