Thread: [Help] A Question About Template Specialization

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    [Help] A Question About Template Specialization

    compiled the following code
    Code:
    class t {
    public:
       template <typename T>
       void tt(T i) { }
       template <>
       void tt(int i) { }
    };
    
    int main()
    {
    }
    and I got the following error message
    5 invalid explicit specialization before '>' token
    5 explicit specialization in non-namespace scope `class t'
    6 invalid member function declaration
    but if I moved function tt out of the class t, say,
    Code:
    template <typename T>
    void tt(T i) { }
    template <>
    void tt(int i) { }
    
    int main()
    {
    }
    then it compiled well.

    I wonder if C++ dosen't support such kind of specialization inside a class? Why? For any good reason?

    Anyway, I do need to declare a template function, and a special version of this function, inside a normal class. How? The only alternative is to make it an overloaded function? say,
    Code:
    class t {
    public:
       template <typename T>
       void tt(T i) { }
    
       void tt(int i) { }
    };
    
    int main()
    {
    }
    then it compiled well
    Last edited by Antigloss; 10-31-2007 at 08:31 AM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You can't partially specialize a template method, any more than you can a template function.

    Partial specialization is for templates CLASSES only. To specialize functions you just use overloading.

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    but as you can see here, this is a complete specialization, not partial.
    Partial specialization is OK for function too.

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    finally, i got how to specialize template methods
    Code:
    class t {
    public:
       template <typename T>
       void tt(T i) { }
    };
    
    template <>
    void t::tt(int i) { }
    
    int main()
    {
    }

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Antigloss View Post
    but as you can see here, this is a complete specialization, not partial.
    Partial specialization is OK for function too.
    No it is not. Try compiling this:

    Code:
    template <typename A, typename B>
    void func(A a, B b) { }
    
    template <typename B>
    void func<int, B>(int a, B b) { }
    The message my compiler gives is:
    t.cc:5: partial specialization `func<int, B>' of function template
    As I said, you can't partially specialize a function template this way. You have to overload it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. Template specialization + linking error
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 06-06-2008, 12:03 PM
  3. Function template specialization?
    By cpjust in forum C++ Programming
    Replies: 17
    Last Post: 02-25-2008, 04:11 PM
  4. Template Partial Specialization
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2007, 11:05 AM
  5. Template specialization
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-19-2002, 07:08 AM