Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Number of Template Parameters in Trait Types? (http://www.velocityreviews.com/forums/t957631-number-of-template-parameters-in-trait-types.html)

Nephi Immortal 02-15-2013 03:16 AM

Number of Template Parameters in Trait Types?
 
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;
}

SG 02-15-2013 08:38 AM

Re: Number of Template Parameters in Trait Types?
 
On Feb 15, 4:16*am, Nephi Immortal wrote:
> 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

Nephi Immortal 02-15-2013 04:37 PM

Re: Number of Template Parameters in Trait Types?
 
On Friday, February 15, 2013 2:38:13 AM UTC-6, SG wrote:
> On Feb 15, 4:16*am, Nephi Immortal wrote:
>
> > 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>
>
> {
>
> };


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?

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




All times are GMT. The time now is 03:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57