Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Finding template parameter types.

Posted on 2009-07-12
12
Medium Priority
?
258 Views
Last Modified: 2012-05-07
Hello.

Evilrix mentioned in a previous question that the Boost library use something as in the code below to simulate variadic templates. What I'm wondering though is how can I check if a template parameter is equal to NoArg and create the internals of the template appropriately? I don't seem to be able to use partial template specialisation as when I do I get errors that default parameters are not allowed with their use. So, how do we go about this, so that something like CMyClass<int> would generate:

CMyClass{
      void method(int);
}

and CMyClass(int, int) would generate:

CMyClass{
      void method(int, int);
}

Thank you,
Uni
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
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
  • 7
  • 5
12 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24834573
Well, this is one way..
http://www.cplusplus.com/reference/std/typeinfo/type_info/

if(typeid(A) == typeid(NoArg))
{
   same thing
}
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24834580
Note, that relies on the type have RTTI info, so it has to have at least one virtual function (oops, forgot to point that out!).
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834581
Hm, I've seen that typeid thing, but I thought that was run-time, not compile-time as templates are?
0
Independent Software Vendors: 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!

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834588
>>so it has to have at least one virtual function (oops, forgot to point that out!).
So I would need something like:

struct NoArg {virutal void rttisupplier(){}};

?

Thanks,
Uni
0
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 24834597
Or the template way -- although this is resolved at compile time and not runtime.
#include <iostream>
 
struct NoArg {};
 
template <typename T>
struct IsNoArg
{
	enum { YES = 0 };
};
 
template <>
struct IsNoArg<NoArg>
{
	enum { YES = 1 };
};
 
template<typename A >
class CMyClass{
public:
	void foo()
	{
		if(IsNoArg<A>::YES)
		{
			std::cout << "Yes" << std::endl;
		}
		else
		{
			std::cout << "No" << std::endl;
		}
	}
};
 
 
int main()
{
	CMyClass<int> c1;
	c1.foo();
 
	CMyClass<NoArg> c2;
	c2.foo();
}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 24834598
>> Hm, I've seen that typeid thing, but I thought that was run-time, not compile-time as templates are?
Yes, runtime -- I was covering all bases for you :)
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31602580
Thank you! I'll have a good look at this... looks interesting... :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834632
Hm, just a follow-up... How could this be used to create differing function declarations though as that "if(IsNoArg<A>::YES)" although is runtime, occurs within the function foo(). See very top of question for further info...

Thanks, :)
Uni
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834707
Actually, no I'll put that in a new question...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24834722
Out right now but I'll see what I can do when I return.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834723
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24834726
No problem mate. :)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
Suggested Courses

636 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