Class Template Member Class Template Specialisaton Query

Discussion in 'C++' started by Simon G Best, Sep 25, 2003.

  1. Simon G Best

    Simon G Best Guest

    Hello!

    I have a query regarding explicit specialisation of class templates
    which are themselves members of class templates.

    Here's what I want to do:

    template< class T > struct pink {

    template< class U > struct floyd;

    };

    template< class T > template< class U >
    struct pink<T>::floyd {

    pink seamus();

    U eugene();

    };

    /* Here's the specialisation I'm wondering
    about (with line numbers given in
    comments on the left): */

    template< class T >
    /*19*/ struct pink<T>::floyd<int> {

    /*21*/ pink seamus();

    float eugene();

    };

    Comeau's online compiler does fine with that :) but GCC 3.2.3 does not
    :-( Instead, g++ says:

    [source file]:19: template parameters not used in partial
    specialization:
    [source file]:19: `T'
    [source file]:21: syntax error before `(' token

    However, I can get it to (sort of) work if I change the specialisation
    definition to:

    template< class T >
    struct pink<T>::floyd<int> {

    pink<T> seamus();

    float eugene();

    };

    though it still warns about T being an unused template parameter.

    I'd just like to confirm my suspicion that GCC 3.2.3 is incorrect on
    this, and that the original version of my code is correct :)

    Thank you!

    Simon
    Simon G Best, Sep 25, 2003
    #1
    1. Advertising

  2. Simon G Best wrote:
    > Hello!
    >
    > I have a query regarding explicit specialisation of class templates
    > which are themselves members of class templates.
    >
    > Here's what I want to do:
    >
    > template< class T > struct pink {
    >
    > template< class U > struct floyd;
    >
    > };
    >
    > template< class T > template< class U >
    > struct pink<T>::floyd {
    >
    > pink seamus();
    >
    > U eugene();
    >
    > };
    >
    > /* Here's the specialisation I'm wondering
    > about (with line numbers given in
    > comments on the left): */
    >
    > template< class T >
    > /*19*/ struct pink<T>::floyd<int> {
    >
    > /*21*/ pink seamus();
    >
    > float eugene();
    >
    > };
    >



    You could theoretically do it with 2 non nested structs.

    template<class T, class U> struct floyd;

    template<class T> struct pink;

    template<class T, class U>
    struct floyd {

    pink<T> seamus();
    U eugene();
    };

    template<class T>
    struct floyd<T,int> {

    pink<T> seamus();
    float eugene();
    };


    > Comeau's online compiler does fine with that :) but GCC 3.2.3 does not
    > :-( Instead, g++ says:
    >
    > [source file]:19: template parameters not used in partial
    > specialization:
    > [source file]:19: `T'
    > [source file]:21: syntax error before `(' token
    >
    > However, I can get it to (sort of) work if I change the specialisation
    > definition to:
    >
    > template< class T >
    > struct pink<T>::floyd<int> {
    >
    > pink<T> seamus();
    >
    > float eugene();
    >
    > };
    >
    > though it still warns about T being an unused template parameter.


    I suspect this is a problem with g++. File a bug with gcc, they're
    usually very responsive.

    >
    > I'd just like to confirm my suspicion that GCC 3.2.3 is incorrect on
    > this, and that the original version of my code is correct :)


    I tried your original version on gcc 3.3.1, you could aloso try a 3.4
    devel version of gcc, they're trying to fix things like this.

    >
    Gianni Mariani, Sep 25, 2003
    #2
    1. Advertising

  3. Simon G Best

    Simon G Best Guest

    Gianni Mariani wrote:
    >
    > You could theoretically do it with 2 non nested structs.


    Yeah, it looks like that's what I'll have to do (though it looks untidy,
    as floyd really belongs to pink) :-(

    (Or I could do interesting things behind the scenes with templates
    outside the containing template, and then define the member template in
    terms of those exterior templates. But I don't like 'clever' solutions
    like that - I prefer to work /with/ the language, rather than try to
    overcome it.)

    [Example of two nonnested templates snipped.]
    >
    > I suspect this is a problem with g++. File a bug with gcc, they're
    > usually very responsive.


    Well, I've since learned (from johnchx in comp.lang.c++.moderated) that
    the standard actually forbids what I was trying to do (14.7.3.18 says
    that it isn't allowed). To explicitly specialise a member class
    template, the enclosing class templates also need to be specialised :-(

    GCC 3.2.3 is actually correctly reporting the error when I try to do it
    the obvious way:

    template< class T > template<>
    struct pink<T>::floyd<int> { /* etc... */ };

    It reports the lack of specialisation of the enclosing class, which is
    consistent with the standard after all (and so my faith in GCC is
    restored :) ).

    > I tried your original version on gcc 3.3.1, you could aloso try a 3.4
    > devel version of gcc, they're trying to fix things like this.


    Thanks :) but it seems I'm going to need to redesign things instead :-(

    Simon
    Simon G Best, Sep 25, 2003
    #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.

Share This Page