Explicit specialisation of function template as member of class template

This is a discussion on Explicit specialisation of function template as member of class template within the C++ Programming forums, part of the General Programming Boards category; Hi, How on earth do I explicitely specialize : Code: template<typename U> class A { public: template<typename T> void f(T& ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    Explicit specialisation of function template as member of class template

    Hi,

    How on earth do I explicitely specialize :
    Code:
    template<typename U>
    class A {
    public:
      template<typename T> void f(T& v)
      {
        // do something in general with u_
      }
      U u_;
    };
    this doesn't work :
    Code:
    template<>
    void A<int> :: f<double> (double& v) {
    // do something special with int
    }
    Thanks !!
    Last edited by MarkZWEERS; 01-30-2011 at 04:19 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Shouldn't the T match the "int"?

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Nope, T should match double'. But you're right, i forgot to specialize f's parameter.

    I've corrected my post, but it doesn't compile. g++ sais :

    new.cpp:67: error: template-id ‘f<double>’ for ‘void A<int>::f(double&)’ does not match any template declaration

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    eureka !

    First I need to indicate an explicit specialization for the class, then for the function. This works :

    Code:
    template<>
    template<>           /// < ---
    void A<int> :: f<double> (double& v) {
    // do something special with int
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling base class function when base is a template
    By VirtualAce in forum C++ Programming
    Replies: 9
    Last Post: 07-11-2010, 03:26 AM
  2. Threading A Member Function For Templated Class
    By scwizzo in forum C++ Programming
    Replies: 12
    Last Post: 01-21-2010, 02:37 AM
  3. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 09:44 AM
  4. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21