Finding template parameter types.

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

LVL 3
Unimatrix_001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Well, this is one way..
http://www.cplusplus.com/reference/std/typeinfo/type_info/

if(typeid(A) == typeid(NoArg))
{
   same thing
}
0
evilrixSenior Software Engineer (Avast)Commented:
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
Unimatrix_001Author Commented:
Hm, I've seen that typeid thing, but I thought that was run-time, not compile-time as templates are?
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Unimatrix_001Author Commented:
>>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
evilrixSenior Software Engineer (Avast)Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
>> 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
Unimatrix_001Author Commented:
Thank you! I'll have a good look at this... looks interesting... :)
0
Unimatrix_001Author Commented:
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
Unimatrix_001Author Commented:
Actually, no I'll put that in a new question...
0
evilrixSenior Software Engineer (Avast)Commented:
Out right now but I'll see what I can do when I return.
0
Unimatrix_001Author Commented:
0
Unimatrix_001Author Commented:
No problem mate. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.