template function specialization inside template class possible?

Discussion in 'C++' started by quarup@gmail.com, Aug 25, 2006.

  1. Guest

    I want to specialize a template function that lives inside a class, but
    am getting a compile error in VS.net 2003. Here's my code:

    template <class T>
    class A {
    public:
    template <class U>
    void f() const;
    };

    template <class T>
    template <>
    void A<T>::f<int>() const {}

    I get the following errors:
    c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    illegal use of explicit template arguments
    c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    illegal use of explicit template arguments
    c:\programming\testCPP\testCPP.cpp(19) : error C2244: 'f' : unable to
    match function definition to an existing declaration
    definition
    'void A<T>::f<int>(void) const'
    existing declarations
    'void A<T>::f(void) const'


    For comparison, here are two situations that DO work:

    1. non-specialized template function inside template class works:

    template <class T>
    class B {
    public:
    template <class U>
    void f() const;
    };

    template <class T>
    template <class U>
    void B<T>::f() const {}

    2. specialized template function inside non-template class works:

    class C {
    public:
    template <class U>
    void f() const;
    };

    template <>
    void C::f<int>() const {}



    Does anyone have any insights? Am I not confirming to the standard, is
    my syntax incorrect, or is VS.net broken?
    , Aug 25, 2006
    #1
    1. Advertising

  2. wrote:
    > I want to specialize a template function that lives inside a class,
    > but am getting a compile error in VS.net 2003. Here's my code:
    >
    > template <class T>
    > class A {
    > public:
    > template <class U>
    > void f() const;
    > };
    >
    > template <class T>
    > template <>
    > void A<T>::f<int>() const {}


    This is not allowed. To specialise a member template of a class
    template you need to first specialise the class template.

    > [..]
    >
    > For comparison, here are two situations that DO work:
    >
    > 1. non-specialized template function inside template class works:
    >
    > template <class T>
    > class B {
    > public:
    > template <class U>
    > void f() const;
    > };
    >
    > template <class T>
    > template <class U>
    > void B<T>::f() const {}
    >
    > 2. specialized template function inside non-template class works:
    >
    > class C {
    > public:
    > template <class U>
    > void f() const;
    > };
    >
    > template <>
    > void C::f<int>() const {}
    >
    >
    >
    > Does anyone have any insights? Am I not confirming to the standard, is
    > my syntax incorrect, or is VS.net broken?


    VS.net is broken in some places but not in this one.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 25, 2006
    #2
    1. Advertising

  3. Alan Johnson Guest

    wrote:
    > I want to specialize a template function that lives inside a class, but
    > am getting a compile error in VS.net 2003. Here's my code:
    >
    > template <class T>
    > class A {
    > public:
    > template <class U>
    > void f() const;
    > };
    >
    > template <class T>
    > template <>
    > void A<T>::f<int>() const {}
    >
    > I get the following errors:
    > c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    > illegal use of explicit template arguments
    > c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    > illegal use of explicit template arguments
    > c:\programming\testCPP\testCPP.cpp(19) : error C2244: 'f' : unable to
    > match function definition to an existing declaration
    > definition
    > 'void A<T>::f<int>(void) const'
    > existing declarations
    > 'void A<T>::f(void) const'
    >
    >
    > For comparison, here are two situations that DO work:
    >
    > 1. non-specialized template function inside template class works:
    >
    > template <class T>
    > class B {
    > public:
    > template <class U>
    > void f() const;
    > };
    >
    > template <class T>
    > template <class U>
    > void B<T>::f() const {}
    >
    > 2. specialized template function inside non-template class works:
    >
    > class C {
    > public:
    > template <class U>
    > void f() const;
    > };
    >
    > template <>
    > void C::f<int>() const {}
    >
    >
    >
    > Does anyone have any insights? Am I not confirming to the standard, is
    > my syntax incorrect, or is VS.net broken?


    You can't specialize a template unless all enclosing templates are also
    specialized. That is, what you are trying to do isn't allowed by the
    standard.

    A workaround to achieve pretty much the same thing is to create a proxy
    class (or function) to do what you want, and specialize that. For
    example:

    #include <iostream>

    class proxy
    {
    public:
    template <class T>
    static void f(T t)
    {
    std::cout << t << std::endl ;
    }

    } ;

    template <>
    void proxy::f<int>(int t)
    {
    std::cout << "(int)" << t << std::endl ;
    }

    template <class T>
    class A
    {
    public:
    template <class U>
    void f(U u)
    {
    proxy::f<U>(u) ;
    }
    } ;

    --
    Alan Johnson
    Alan Johnson, Aug 25, 2006
    #3
  4. Alan Johnson wrote:
    > wrote:
    >> I want to specialize a template function that lives inside a class,
    >> but am getting a compile error in VS.net 2003. Here's my code:
    >>
    >> template <class T>
    >> class A {
    >> public:
    >> template <class U>
    >> void f() const;
    >> };
    >>
    >> template <class T>
    >> template <>
    >> void A<T>::f<int>() const {}
    >>
    >> I get the following errors:
    >> c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    >> illegal use of explicit template arguments
    >> c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    >> illegal use of explicit template arguments
    >> c:\programming\testCPP\testCPP.cpp(19) : error C2244: 'f' : unable to
    >> match function definition to an existing declaration
    >> definition
    >> 'void A<T>::f<int>(void) const'
    >> existing declarations
    >> 'void A<T>::f(void) const'
    >>
    >>
    >> For comparison, here are two situations that DO work:
    >>
    >> 1. non-specialized template function inside template class works:
    >>
    >> template <class T>
    >> class B {
    >> public:
    >> template <class U>
    >> void f() const;
    >> };
    >>
    >> template <class T>
    >> template <class U>
    >> void B<T>::f() const {}
    >>
    >> 2. specialized template function inside non-template class works:
    >>
    >> class C {
    >> public:
    >> template <class U>
    >> void f() const;
    >> };
    >>
    >> template <>
    >> void C::f<int>() const {}
    >>
    >>
    >>
    >> Does anyone have any insights? Am I not confirming to the standard,
    >> is my syntax incorrect, or is VS.net broken?

    >
    > You can't specialize a template unless all enclosing templates are
    > also specialized. That is, what you are trying to do isn't allowed
    > by the standard.
    >
    > A workaround to achieve pretty much the same thing is to create a
    > proxy class (or function) to do what you want, and specialize that.
    > For example:
    >
    > #include <iostream>
    >
    > class proxy
    > {
    > public:
    > template <class T>
    > static void f(T t)
    > {
    > std::cout << t << std::endl ;
    > }
    >
    > } ;
    >
    > template <>
    > void proxy::f<int>(int t)
    > {
    > std::cout << "(int)" << t << std::endl ;
    > }
    >
    > template <class T>
    > class A
    > {
    > public:
    > template <class U>
    > void f(U u)
    > {
    > proxy::f<U>(u) ;
    > }
    > } ;


    Actually, if the purpose is to provide a function that takes the
    argument of type 'int' (or something like that), a simple
    overloading will do:

    template <class T>
    class A
    {
    public:
    template <class U>
    void f(U u)
    {
    // generic ipmlementation
    }
    void f(int i)
    {
    // int-specific implementation
    }
    };

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 25, 2006
    #4
  5. Guest

    Thanks, guys, that's a huge help. Your proxy is an elegant workaround!

    Cheers,
    -Q

    Alan Johnson wrote:
    > wrote:
    > > I want to specialize a template function that lives inside a class, but
    > > am getting a compile error in VS.net 2003. Here's my code:
    > >
    > > template <class T>
    > > class A {
    > > public:
    > > template <class U>
    > > void f() const;
    > > };
    > >
    > > template <class T>
    > > template <>
    > > void A<T>::f<int>() const {}
    > >
    > > I get the following errors:
    > > c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    > > illegal use of explicit template arguments
    > > c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    > > illegal use of explicit template arguments
    > > c:\programming\testCPP\testCPP.cpp(19) : error C2244: 'f' : unable to
    > > match function definition to an existing declaration
    > > definition
    > > 'void A<T>::f<int>(void) const'
    > > existing declarations
    > > 'void A<T>::f(void) const'
    > >
    > >
    > > For comparison, here are two situations that DO work:
    > >
    > > 1. non-specialized template function inside template class works:
    > >
    > > template <class T>
    > > class B {
    > > public:
    > > template <class U>
    > > void f() const;
    > > };
    > >
    > > template <class T>
    > > template <class U>
    > > void B<T>::f() const {}
    > >
    > > 2. specialized template function inside non-template class works:
    > >
    > > class C {
    > > public:
    > > template <class U>
    > > void f() const;
    > > };
    > >
    > > template <>
    > > void C::f<int>() const {}
    > >
    > >
    > >
    > > Does anyone have any insights? Am I not confirming to the standard, is
    > > my syntax incorrect, or is VS.net broken?

    >
    > You can't specialize a template unless all enclosing templates are also
    > specialized. That is, what you are trying to do isn't allowed by the
    > standard.
    >
    > A workaround to achieve pretty much the same thing is to create a proxy
    > class (or function) to do what you want, and specialize that. For
    > example:
    >
    > #include <iostream>
    >
    > class proxy
    > {
    > public:
    > template <class T>
    > static void f(T t)
    > {
    > std::cout << t << std::endl ;
    > }
    >
    > } ;
    >
    > template <>
    > void proxy::f<int>(int t)
    > {
    > std::cout << "(int)" << t << std::endl ;
    > }
    >
    > template <class T>
    > class A
    > {
    > public:
    > template <class U>
    > void f(U u)
    > {
    > proxy::f<U>(u) ;
    > }
    > } ;
    >
    > --
    > Alan Johnson
    , Aug 25, 2006
    #5
  6. Guest

    > Actually, if the purpose is to provide a function that takes the
    > argument of type 'int' (or something like that), a simple
    > overloading will do:


    You're right! Thanks, that looks even cleaner. I'll also mentally store
    the proxy solution in my bag of tricks.

    Victor Bazarov wrote:
    > Alan Johnson wrote:
    > > wrote:
    > >> I want to specialize a template function that lives inside a class,
    > >> but am getting a compile error in VS.net 2003. Here's my code:
    > >>
    > >> template <class T>
    > >> class A {
    > >> public:
    > >> template <class U>
    > >> void f() const;
    > >> };
    > >>
    > >> template <class T>
    > >> template <>
    > >> void A<T>::f<int>() const {}
    > >>
    > >> I get the following errors:
    > >> c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    > >> illegal use of explicit template arguments
    > >> c:\programming\testCPP\testCPP.cpp(19) : error C2768: 'A<T>::f' :
    > >> illegal use of explicit template arguments
    > >> c:\programming\testCPP\testCPP.cpp(19) : error C2244: 'f' : unable to
    > >> match function definition to an existing declaration
    > >> definition
    > >> 'void A<T>::f<int>(void) const'
    > >> existing declarations
    > >> 'void A<T>::f(void) const'
    > >>
    > >>
    > >> For comparison, here are two situations that DO work:
    > >>
    > >> 1. non-specialized template function inside template class works:
    > >>
    > >> template <class T>
    > >> class B {
    > >> public:
    > >> template <class U>
    > >> void f() const;
    > >> };
    > >>
    > >> template <class T>
    > >> template <class U>
    > >> void B<T>::f() const {}
    > >>
    > >> 2. specialized template function inside non-template class works:
    > >>
    > >> class C {
    > >> public:
    > >> template <class U>
    > >> void f() const;
    > >> };
    > >>
    > >> template <>
    > >> void C::f<int>() const {}
    > >>
    > >>
    > >>
    > >> Does anyone have any insights? Am I not confirming to the standard,
    > >> is my syntax incorrect, or is VS.net broken?

    > >
    > > You can't specialize a template unless all enclosing templates are
    > > also specialized. That is, what you are trying to do isn't allowed
    > > by the standard.
    > >
    > > A workaround to achieve pretty much the same thing is to create a
    > > proxy class (or function) to do what you want, and specialize that.
    > > For example:
    > >
    > > #include <iostream>
    > >
    > > class proxy
    > > {
    > > public:
    > > template <class T>
    > > static void f(T t)
    > > {
    > > std::cout << t << std::endl ;
    > > }
    > >
    > > } ;
    > >
    > > template <>
    > > void proxy::f<int>(int t)
    > > {
    > > std::cout << "(int)" << t << std::endl ;
    > > }
    > >
    > > template <class T>
    > > class A
    > > {
    > > public:
    > > template <class U>
    > > void f(U u)
    > > {
    > > proxy::f<U>(u) ;
    > > }
    > > } ;

    >
    > Actually, if the purpose is to provide a function that takes the
    > argument of type 'int' (or something like that), a simple
    > overloading will do:
    >
    > template <class T>
    > class A
    > {
    > public:
    > template <class U>
    > void f(U u)
    > {
    > // generic ipmlementation
    > }
    > void f(int i)
    > {
    > // int-specific implementation
    > }
    > };
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    , Aug 25, 2006
    #6
  7. Guest

    Alan Johnson wrote:
    > A workaround to achieve pretty much the same thing is to create a proxy
    > class (or function) to do what you want, and specialize that. For
    > example:


    Why would you use a class at all here? It's not obvious to me how it
    provides an advantage over simply having templated functions that your
    member functions call. Maybe if you friend'd the proxy class and passed
    the this pointer to its member functions, so you could write code
    similar to having the member functions in the original class?
    , Aug 25, 2006
    #7
  8. Guest

    > It's not obvious to me how it
    > provides an advantage over simply having templated functions that your
    > member functions call.


    You're right. The problem is that you can't have specialized templated
    functions inside non-fully specialized template classes (apparently not
    part of the standard), which is the reason the thread was started. The
    proxy is just a workaround.

    wrote:
    > Alan Johnson wrote:
    > > A workaround to achieve pretty much the same thing is to create a proxy
    > > class (or function) to do what you want, and specialize that. For
    > > example:

    >
    > Why would you use a class at all here? It's not obvious to me how it
    > provides an advantage over simply having templated functions that your
    > member functions call. Maybe if you friend'd the proxy class and passed
    > the this pointer to its member functions, so you could write code
    > similar to having the member functions in the original class?
    , Aug 26, 2006
    #8
    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,645
    pdixtl
    Jun 4, 2010
  2. Ruben Campos
    Replies:
    3
    Views:
    6,447
  3. Joseph Turian
    Replies:
    4
    Views:
    566
    John Carson
    Mar 20, 2006
  4. Joseph Turian
    Replies:
    2
    Views:
    454
  5. Replies:
    2
    Views:
    333
Loading...

Share This Page