Can't figure out specialziation syntax for g++.

Discussion in 'C++' started by John, Nov 30, 2006.

  1. John

    John Guest

    Heya -

    The following works with MSVC:

    template< bool Boolean >
    struct Foo
    {
    template< class T2 >
    void bar( T2 arg )
    {
    std::cout << arg << std::endl;
    }

    template<>
    void bar( bool arg )
    {
    if ( Boolean )
    std::cout << arg << std::endl;
    }

    };

    Of course, g++ complains about "explicit specialization in non-namespace
    scope" - this is usually no big deal, just move the specialization
    outside of th class, right? For this example, I'm unable to figure out
    how to move the specialization outside of the class definition without
    generating a raft of errors, with either compiler. Basically I think
    this is some type of partial specialization, but the wrench is the
    'bool' as the template parameter of the class.

    The closest I've been able to come up with is:

    template<>
    template< bool Boolean >
    inline void Foo<bool>::bar( bool arg )
    {
    }

    but it doesn't seem to work.

    Any help would be greatly appreciated. :)

    - John
     
    John, Nov 30, 2006
    #1
    1. Advertising

  2. John

    John Carson Guest

    "John" <> wrote in message
    news:pyBbh.10346$
    > Heya -
    >
    > The following works with MSVC:
    >
    > template< bool Boolean >
    > struct Foo
    > {
    > template< class T2 >
    > void bar( T2 arg )
    > {
    > std::cout << arg << std::endl;
    > }
    >
    > template<>
    > void bar( bool arg )
    > {
    > if ( Boolean )
    > std::cout << arg << std::endl;
    > }
    >
    > };
    >
    > Of course, g++ complains about "explicit specialization in
    > non-namespace scope" - this is usually no big deal, just move the
    > specialization outside of th class, right? For this example, I'm
    > unable to figure out how to move the specialization outside of the
    > class definition without generating a raft of errors, with either
    > compiler. Basically I think this is some type of partial
    > specialization, but the wrench is the 'bool' as the template
    > parameter of the class.
    > The closest I've been able to come up with is:
    >
    > template<>
    > template< bool Boolean >
    > inline void Foo<bool>::bar( bool arg )
    > {
    > }
    >
    > but it doesn't seem to work.
    >
    > Any help would be greatly appreciated. :)



    What you are trying to do is not allowed by the standard, though VC++ does
    allow it for backward compatibility reasons.

    According to the standard, you are only allowed to specialise member
    templates if the enclosing template class is fully specialised. Something
    like this:

    template< bool Boolean >
    struct Foo
    {
    template< class T2 >
    void bar( T2 arg )
    {
    std::cout << arg << std::endl;
    }
    };

    template<>
    template<>
    void Foo<true>::bar<bool>( bool arg )
    {
    // Foo<true> case
    }

    template<>
    template<>
    void Foo<false>::bar<bool>( bool arg )
    {
    // Foo<false> case
    }


    --
    John Carson
     
    John Carson, Nov 30, 2006
    #2
    1. Advertising

  3. John

    John Guest

    John Carson wrote:
    > "John" <> wrote in message
    > news:pyBbh.10346$
    >> Heya -

    > According to the standard, you are only allowed to specialise member
    > templates if the enclosing template class is fully specialised. Something
    > like this:


    Awsome! I can deal with that.

    Thanks!
     
    John, Nov 30, 2006
    #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. logia
    Replies:
    6
    Views:
    651
  2. python newbie

    can't figure out syntax prob

    python newbie, Nov 22, 2003, in forum: Python
    Replies:
    2
    Views:
    303
    python newbie
    Nov 22, 2003
  3. Aaron Walker
    Replies:
    4
    Views:
    714
    Aaron Walker
    Oct 1, 2005
  4. Albert Hopkins
    Replies:
    3
    Views:
    470
    Steven D'Aprano
    Dec 10, 2008
  5. Tim
    Replies:
    2
    Views:
    119
Loading...

Share This Page