Number of Template Parameters in Trait Types?

N

Nephi Immortal

I can create trait types with the fixed number of template parameters. How can I define different number of parameters in the same class name? I know that it is not legal, but can be done in trick.

enum E
{
eA,
eB
};

template< E e, typename T >
struct A
{
};

template< E e, typename T, typename T2 >
struct A
{
};


int main()
{
A< eA, long > a;
A< eB, short, long > a2;

return 0;
}
 
S

SG

I can create trait types with the fixed number of template parameters.
How can I define different number of parameters in the same class
name?

What problem are you trying to solve like that?
enum E
{
    eA, eB
};

template< E e, typename T >
struct A
{
};

template< E e, typename T, typename T2 >
struct A
{
};

int main()
{
    A< eA, long > a;
    A< eB, short, long > a2;
    return 0;
}

C++11 allows something like that via variadic templates:

template<E e, typename T1, typename...More >
struct A

Here, "More" is a template parameter pack containing zero or more
parameters. It can be expanded using the ellipsis on the right-hand
side of More:

{
std::tuple<T1,More...> stuff;
};

Alternativly, you can do partial specialization:

template<E e, typename T1, typename...More >
struct A;

template<E e, typename T1>
struct A<e,T1>
{
};

template<E e, typename T1, typename T2>
struct A<e,T1,T2>
{
};

But I would try to avoid that and define the class template
generically if possible.

Cheers!
SG
 
N

Nephi Immortal

What problem are you trying to solve like that?











C++11 allows something like that via variadic templates:



template<E e, typename T1, typename...More >

struct A



Here, "More" is a template parameter pack containing zero or more

parameters. It can be expanded using the ellipsis on the right-hand

side of More:



{

std::tuple<T1,More...> stuff;

};



Alternativly, you can do partial specialization:



template<E e, typename T1, typename...More >

struct A;



template<E e, typename T1>

struct A<e,T1>

{

};



template<E e, typename T1, typename T2>

struct A<e,T1,T2>

{

};

Yes, that is what I mean. Why didn't Visual C++ compile and show errors?
The code you posted here is not supported. Do GCC Compiler support?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top