Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7508021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:36:09+00:00 2026-05-29T22:36:09+00:00

In C++, I am trying to specialize a templated function for a object that

  • 0

In C++, I am trying to specialize a templated function for a object that is templated itself.

Here is a basic example:
test.h:

template <class T>
class myC {
    T x;
};

template <class U>
void f(U y) {
}

template <>
template <class T>
void f<myC<T> >(myC<T> y) {
}

test.cpp

#include "test.h"
int main() {
    myC<double> m;
    f(m);
}

GCC 4.6.1 gives me the following error message:

In file included from test.cpp:1:0:
test.h:13:25: error: too many template parameter lists in declaration of ‘void f(myC<T>)’
test.h:13:6: error: template-id ‘f<myC<T> >’ for ‘void f(myC<T>)’ does not match any template declaration
test.h:13:25: note: saw 2 ‘template<>’, need 1 for specializing a member function template

Is this at all possible? Or is there another way to accomplish the same goal?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:36:10+00:00Added an answer on May 29, 2026 at 10:36 pm

    You cannot specialize a template function; only template classes can be specialized. Edit: Nawaz’s answer is correct: it’s partial specialization that is not allowed for template functions, only for classes. A full specialization is possible:

    template <class U> void f(U y) {}
    template<> void f<double>(double y) {} // specialization for double
    

    Note that the template argument need not be explicitly specified if it can be deduced from the context:

    template<> void f<>(int y) {} // specialization for int
    

    In your case, full specialization is not possible because the function argument is a template class. However, a template function, like any function, can be overloaded. In your case, it will be like this:

    template <class T>
    class myC {
        T x;
    };
    
    template <class U>
    void f(U y) {
    }
    
    template <class T>
    void f(myC<T> y) {
    }
    
    int main() {
        myC<double> m;
        f(m);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to implement a function that needs partial template specializations and fallen
I am trying to implement a template function with handles void differently using template
I'm trying to specialize a template method of non-template class in its subclass: //
I have a templated class with an templated member function template<class T> class A
I'm trying to specialize just one method of a class template . The accepted
I am trying to specialize template the following way: template<size_t _1,size_t _2> // workaround:
I'm trying to serialize a custom class that needs to use multiple elements of
I'm trying to serialize an object to XML that has a number of properties,
I'm trying to write code that uses a member typedef of a template argument,
I'm trying to use partial template specialization on a (non-member) function, and I'm tripping

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.