Solved

Finding template parameter types.

Posted on 2009-07-12
12
226 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 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

16 Experts available now in Live!

Get 1:1 Help Now