Defining templated member function outside templated class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chhenning

    Defining templated member function outside templated class

    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
  • Ian Collins

    #2
    Re: Defining templated member function outside templated class

    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.

    Comment

    • Bo Persson

      #3
      Re: Defining templated member function outside templated class

      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<Kwould 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


      Comment

      • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

        #4
        Re: Defining templated member function outside templated class

        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

        Comment

        • Ian Collins

          #5
          Re: Defining templated member function outside templated class

          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.

          Comment

          • chhenning

            #6
            Re: Defining templated member function outside templated class

            Thank you all!


            On Feb 13, 1:34 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            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 -

            Comment

            Working...