Solved

Finding template parameter types.

Posted on 2009-07-12
12
248 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
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!

 
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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

733 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