Solved

Finding template parameter types.

Posted on 2009-07-12
12
218 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.

760 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