Specialization of member function template in template class?

Discussion in 'C++' started by Joseph Turian, Mar 19, 2006.

  1. Hi,

    What is the correct syntax to get the bar<T>::f<int, unsigned>()
    function to compile in the following fragment?

    Thanks,
    Joseph


    class foo {
    template<typename A, typename B> void f();
    };

    template<typename T>
    class bar {
    template<typename A, typename B> void f();
    };

    template<typename A, typename B>
    void foo::f() { }

    template<>
    void foo::f<int, unsigned>() { }


    template <typename T>
    template<typename A, typename B>
    void bar<T>::f() { }

    template <typename T>
    template<>
    void bar<T>::f<int, unsigned>() { }
     
    Joseph Turian, Mar 19, 2006
    #1
    1. Advertising

  2. Joseph Turian

    Guest

    i also do not know how to writer this functions
    but this way is simple:
    template<typename T>
    class bar {
    template<typename A, typename B>
    void f(){}
    template<>
    f<int, unsigned>() { }
    };
    V
     
    , Mar 20, 2006
    #2
    1. Advertising

  3. wrote:
    > i also do not know how to writer this functions
    > but this way is simple:


    I know this method too.
    However, I have a bunch of these functions and it would be unwieldly to
    put them in the class declaration.
    Hence, I would like to know the syntax for having the implementation
    outside.

    Joseph


    > template<typename T>
    > class bar {
    > template<typename A, typename B>
    > void f(){}
    > template<>
    > f<int, unsigned>() { }
    > };
    > V
     
    Joseph Turian, Mar 20, 2006
    #3
  4. Joseph Turian

    Axter Guest

    Joseph Turian wrote:
    > Hi,
    >
    > What is the correct syntax to get the bar<T>::f<int, unsigned>()
    > function to compile in the following fragment?
    >
    > Thanks,
    > Joseph
    >
    >
    > class foo {
    > template<typename A, typename B> void f();
    > };
    >
    > template<typename T>
    > class bar {
    > template<typename A, typename B> void f();
    > };
    >
    > template<typename A, typename B>
    > void foo::f() { }
    >
    > template<>
    > void foo::f<int, unsigned>() { }
    >
    >
    > template <typename T>
    > template<typename A, typename B>
    > void bar<T>::f() { }
    >
    > template <typename T>
    > template<>
    > void bar<T>::f<int, unsigned>() { }


    That syntax looks good to me, but the Comeau compiler gives the
    following error:
    a template declaration containing a template parameter list
    may not
    be followed by an explicit specialization declaration
    template<>
    ^

    It seems like it's indicating that the standard doesn't allow this out
    side of the class declaration.
    IMHO, it's far easier to read template code that includes the
    implementation inside the class declaration.

    IMO, template methods located outside the class declaration looks
    unwieldy, and makes for problematic maintenance.
     
    Axter, Mar 20, 2006
    #4
  5. Joseph Turian

    John Carson Guest

    "Joseph Turian" <> wrote in message
    news:
    > wrote:
    >> i also do not know how to writer this functions
    >> but this way is simple:

    >
    > I know this method too.
    > However, I have a bunch of these functions and it would be unwieldly
    > to put them in the class declaration.
    > Hence, I would like to know the syntax for having the implementation
    > outside.
    >
    > Joseph
    >
    >
    >> template<typename T>
    >> class bar {
    >> template<typename A, typename B>
    >> void f(){}
    >> template<>
    >> f<int, unsigned>() { }
    >> };
    >> V


    VC++ allows you to do this for some backward compatibility reason, but it is
    non-standard. According to the standard, you can only specialize member
    templates at namespace scope, i.e., outside the class declaration.

    Further, you can only specialize member templates if the enclosing class is
    fully specialized. What you are trying to do --- specialize a member
    template while leaving the enclosing class un-specialized --- is simply not
    allowed by the standard.

    Relevant sections from the standard are as follows:

    Section 14.7.3/18:

    "In an explicit specialization declaration for a member of a class template
    or a member template that appears in namespace scope, the member template
    and some of its enclosing class templates may remain unspecialized, except
    that the declaration shall not explicitly specialize a class member template
    if its enclosing class templates are not explicitly specialized as well."

    You may note that this refers to specializations that appear in namespace
    scope. Section 14.7.3/2 makes it clear that this is the only option:

    "An explicit specialization shall be declared in the namespace of which the
    template is a member, or, for member templates, in the namespace of which
    the enclosing class or enclosing class template is a member."

    --
    John Carson
     
    John Carson, Mar 20, 2006
    #5
    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. Dave
    Replies:
    4
    Views:
    7,710
    pdixtl
    Jun 4, 2010
  2. Ruben Campos
    Replies:
    3
    Views:
    6,495
  3. Bjoern Knafla
    Replies:
    2
    Views:
    3,839
    Bjoern Knafla
    Feb 16, 2005
  4. BigMan
    Replies:
    1
    Views:
    529
  5. toton
    Replies:
    2
    Views:
    347
    toton
    Feb 22, 2008
Loading...

Share This Page