Number of Template Parameters in Trait Types?

Discussion in 'C++' started by Nephi Immortal, Feb 15, 2013.

  1. 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;
    }
    Nephi Immortal, Feb 15, 2013
    #1
    1. Advertising

  2. Nephi Immortal

    SG Guest

    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
    SG, Feb 15, 2013
    #2
    1. Advertising

  3. 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
    Nephi Immortal, Feb 15, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Guio

    trait technique?

    Patrick Guio, Dec 18, 2004, in forum: C++
    Replies:
    7
    Views:
    369
    Jonathan Mcdougall
    Dec 21, 2004
  2. abir
    Replies:
    3
    Views:
    335
    Triple-DES
    Feb 27, 2008
  3. abir
    Replies:
    2
    Views:
    315
  4. kito
    Replies:
    2
    Views:
    413
  5. Thufir
    Replies:
    2
    Views:
    93
    Thufir
    Nov 13, 2007
Loading...

Share This Page