Out-of-class definition of a non-template member of an explicitly specialized class template?

Discussion in 'C++' started by BigMan, Apr 21, 2005.

  1. BigMan

    BigMan Guest

    Why cannot I define a member of an explicitly specialized class
    template out of the class template specialization:

    template< int i >
    struct a
    {
    static void Do( );
    };

    template< >
    struct a< 2 >
    {
    static void Do( ); // in-class definition would be OK...
    };

    // .. however, neither VC 7.1, nor g++ 3.4.2,
    // accept out-of-class definition:
    template< >
    void a< 2 >::Do( )
    {
    }
     
    BigMan, Apr 21, 2005
    #1
    1. Advertising

  2. BigMan

    CrayzeeWulf Guest

    BigMan wrote:

    > // .. however, neither VC 7.1, nor g++ 3.4.2,
    > // accept out-of-class definition:
    > template< >
    > void a< 2 >::Do( )
    > {
    > }

    Because, "a<2>::Do()" is not a template. Try:

    void a<2>::Do()
    {

    }

    (i.e. without "template<>" at the top).

    Thanks,
    --
    CrayzeeWulf
     
    CrayzeeWulf, Apr 21, 2005
    #2
    1. Advertising

  3. Re: Out-of-class definition of a non-template member of an explicitlyspecialized class template?

    BigMan wrote:
    >
    > Why cannot I define a member of an explicitly specialized class
    > template out of the class template specialization:
    >
    > template< int i >
    > struct a
    > {
    > static void Do( );
    > };
    >
    > template< >
    > struct a< 2 >
    > {
    > static void Do( ); // in-class definition would be OK...
    > };
    >
    > // .. however, neither VC 7.1, nor g++ 3.4.2,
    > // accept out-of-class definition:
    > template< >
    > void a< 2 >::Do( )
    > {
    > }
    > ...


    'template <>' is used to prefix explicit specializations. If you wanted
    to explicitly specialize 'a<i>::Do()' for 'i = 2' without explicitly
    specializing the entire class 'a', you could do it as follows

    template<int i> struct a {
    static void Do( );
    };

    template<> void a<2>::Do() {
    /* ... */
    }

    But it your code you decided to perform explicit specialization of the
    entire class 'a'

    template<> struct a<2> {
    static void Do( );
    };

    In this case the out-of-class definition of 'a<2>::Do' has to use the
    "regular" syntax

    void a<2>::Do() {
    /* ... */
    }

    It cannot be prefixed with 'template<>' in this case.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Apr 22, 2005
    #3
  4. BigMan

    BigMan Guest

    Thanks a lot to both of you!
     
    BigMan, Apr 22, 2005
    #4
    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.

Share This Page