Defining templated member function outside templated class

Discussion in 'C++' started by chhenning, Feb 13, 2008.

  1. chhenning

    chhenning Guest

    Hi there, I'm surprised the following construct seems to be illegal:

    template< typename T >
    struct A
    {
    template< typename K >
    void do_some( const K& );
    };

    template< typename T >
    template< typename K >
    void A<T>::do_some<K>( const K& k )
    {}

    I have tried several compilers, include Comeau and they all fail to
    compile. Is the above code not allowed by the current standard?

    Regards,
    Christian
    chhenning, Feb 13, 2008
    #1
    1. Advertising

  2. chhenning

    Ian Collins Guest

    chhenning wrote:
    > Hi there, I'm surprised the following construct seems to be illegal:
    >
    > template< typename T >
    > struct A
    > {
    > template< typename K >
    > void do_some( const K& );
    > };
    >
    > template< typename T >
    > template< typename K >
    > void A<T>::do_some<K>( const K& k )
    > {}
    >

    template< typename T >
    template< typename K >
    void A<T>::do_some( const K& k )
    {}

    --
    Ian Collins.
    Ian Collins, Feb 13, 2008
    #2
    1. Advertising

  3. chhenning

    Bo Persson Guest

    chhenning wrote:
    > Hi there, I'm surprised the following construct seems to be illegal:
    >
    > template< typename T >
    > struct A
    > {
    > template< typename K >
    > void do_some( const K& );
    > };
    >
    > template< typename T >
    > template< typename K >
    > void A<T>::do_some<K>( const K& k )
    > {}
    >
    > I have tried several compilers, include Comeau and they all fail to
    > compile. Is the above code not allowed by the current standard?
    >


    No, but only becase it is wrong. :)

    The code do_some<K> would indicate a specialization of the function,
    which is not allowed. You should instead try:

    template< typename T >
    template< typename K >
    void A<T>::do_some( const K& k )
    {}



    Bo Persson
    Bo Persson, Feb 13, 2008
    #3
  4. On 2008-02-13 19:04, chhenning wrote:
    > Hi there, I'm surprised the following construct seems to be illegal:
    >
    > template< typename T >
    > struct A
    > {
    > template< typename K >
    > void do_some( const K& );
    > };
    >
    > template< typename T >
    > template< typename K >
    > void A<T>::do_some<K>( const K& k )
    > {}
    >
    > I have tried several compilers, include Comeau and they all fail to
    > compile. Is the above code not allowed by the current standard?


    I do not know why this is illegal (neither the motivation nor the
    chapter and verse of the standard) but the following works (in VC++ 2008
    Express):

    template< typename T >
    template< typename K >
    void A<T>::do_some( const K& k )
    {}

    --
    Erik Wikström
    Erik Wikström, Feb 13, 2008
    #4
  5. chhenning

    Ian Collins Guest

    Erik Wikström wrote:
    > On 2008-02-13 19:04, chhenning wrote:
    >> Hi there, I'm surprised the following construct seems to be illegal:
    >>
    >> template< typename T >
    >> struct A
    >> {
    >> template< typename K >
    >> void do_some( const K& );
    >> };
    >>
    >> template< typename T >
    >> template< typename K >
    >> void A<T>::do_some<K>( const K& k )
    >> {}
    >>
    >> I have tried several compilers, include Comeau and they all fail to
    >> compile. Is the above code not allowed by the current standard?

    >
    > I do not know why this is illegal (neither the motivation nor the
    > chapter and verse of the standard) but the following works (in VC++ 2008
    > Express):
    >

    It appears as a partial specialisation.

    --
    Ian Collins.
    Ian Collins, Feb 13, 2008
    #5
  6. chhenning

    chhenning Guest

    Thank you all!


    On Feb 13, 1:34 pm, Ian Collins <> wrote:
    > Erik Wikström wrote:
    > > On 2008-02-13 19:04, chhenning wrote:
    > >> Hi there, I'm surprised the following construct seems to be illegal:

    >
    > >> template< typename T >
    > >> struct A
    > >> {
    > >>    template< typename K >
    > >>    void do_some( const K& );
    > >> };

    >
    > >> template< typename T >
    > >> template< typename K >
    > >> void A<T>::do_some<K>( const K& k )
    > >> {}

    >
    > >> I have tried several compilers, include Comeau and they all fail to
    > >> compile. Is the above code not allowed by the current standard?

    >
    > > I do not know why this is illegal (neither the motivation nor the
    > > chapter and verse of the standard) but the following works (in VC++ 2008
    > > Express):

    >
    > It appears as a partial specialisation.
    >
    > --
    > Ian Collins.- Hide quoted text -
    >
    > - Show quoted text -
    chhenning, Feb 13, 2008
    #6
    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. jt
    Replies:
    13
    Views:
    945
    grahamo
    Apr 5, 2005
  2. Jahagirdar Vijayvithal S
    Replies:
    2
    Views:
    434
    Jahagirdar Vijayvithal S
    Aug 7, 2005
  3. Replies:
    1
    Views:
    2,678
    =?ISO-8859-1?Q?Stefan_N=E4we?=
    Jan 9, 2006
  4. Amadeus W. M.
    Replies:
    2
    Views:
    391
    Amadeus W. M.
    Jul 4, 2006
  5. Replies:
    1
    Views:
    302
Loading...

Share This Page