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 5941641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:08:20+00:00 2026-05-22T16:08:20+00:00

Note: this seems to be a repost of a problem: C++ – Overload templated

  • 0

Note: this seems to be a repost of a problem: C++ – Overload templated class method with a partial specilization of that method

I have boiled down a problem I am having with C++ template specialization down to a simple case.

It consists of a simple 2-parameter template class Thing, where I would like to specialize Thing<A,B>::doSomething() for B=int.

#include <cstdio>

// A 3-parameter template class.
template <class A, class B>
class Thing
{
public:
    Thing(A a, B b) : a_(a), b_(b) {}
    B doSomething();
private:
    A a_;
    B b_;
};

// The generic case works as expected.
template <class A, class B>
B Thing<A,B>::doSomething()
{
    return b_;
}

// This specialization does not work!
template <class A>
int Thing<A,int>::doSomething()
{
    return b_+1;
}

int main() {
    // Setup our thing.
    Thing<double,int> thing(1.0,2);
    // This doesn't compile - but works with the generic case.
    printf("Expecting 3, and getting %i\n", thing.doSomething());
    // Clean up.
    return 0;
}

Unfortunately, g++ exits with the error:

partial_specialization.cpp:30: error: invalid use of incomplete type ‘class Thing<A, int>’
partial_specialization.cpp:8: error: declaration of ‘class Thing<A, int>’

The clang++ compiler is a bit more verbose, but has the same problem:

partial_specialization.cpp:30:19: error: nested name specifier 'Thing<A, int>::' for declaration does not
      refer into a class, class template or class template partial specialization
int Thing<A,int>::doSomething()
    ~~~~~~~~~~~~~~^
partial_specialization.cpp:32:12: error: use of undeclared identifier 'b_'
    return b_+1;
           ^
2 errors generated.

I have read and understood that partial template specializations on functions aren’t allowed – but I thought I was partially specializing over classes of Thing in this case.

Any ideas?

What I did: A workaround, as determined from the link provided by the accepted answer:

template< class T >
inline T foo( T const & v ) { return v; }

template<>
inline int foo( int const & v ) { return v+1; }

// The generic case works as expected.
template <class A, class B>
B Thing<A,B>::doSomething()
{
    return foo(b_);
}
  • 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-22T16:08:20+00:00Added an answer on May 22, 2026 at 4:08 pm

    Partial specialization of a function template, whether it is member function template or stand-alone function template, is not allowed by the Standard:

    template<typename T, typename U> void f() {} //okay  - primary template
    template<typename T> void f<T,int>() {}      //error - partial specialization
    template<> void f<unsigned char,int>() {}    //okay  - full specialization
    

    But you can partially specialize the class template itself. You can do something like this:

    template <class A>
    class Thing<A,int>  //partial specialization of the class template
    {
        //..
        int doSomething();
    };
    
    template <class A>
    int Thing<A,int>::doSomething()  { /* do whatever you want to do here */ }
    

    Note that when you partially specialize a class template, then the template parameter-list of member function (in its definition outside the class), must match the template parameter list of the class template partial specialization. That means, for the above partial specialization of the class template, you cannot define this:

    template <class A>
    int Thing<A,double>::doSomething(); //error
    

    Its not allowed, because the template parameter-list in function definition didn’t match the template parameter-list of the class template partial specialization. §14.5.4.3/1 from the Standard (2003) says,

    The template parameter list of a member of a class template partial specialization shall match the template parameter list of the class template partial specialization.[…]

    For more on this, read my answer here:

    C++ – Overload templated class method with a partial specilization of that method


    So what is the solution? Would you partially specialize your class along with all the repetitive work?

    A simple solution would be work delegation, instead of partially specializing the class template. Write a stand-alone function template and specialize this as:

    template <class B>
    B doTheActualSomething(B & b) { return b;  }
    
    template <>
    int doTheActualSomething<int>(int & b) { return b + 1; }
    

    And then call this function template from doSomething() member function as:

    template <class A, class B>
    B Thing<A,B>::doSomething() { return doTheActualSomething<B>(b_); }
    

    Since in your particular case, doTheActualSomething needs to know the value of only one member, namely b_, the above solution is fine, as you can pass the value to the function as argument whose type is the template type argument B, and specialization for int is possible being it full-specialization.

    But imagine if it needs to access multiple members, type of each depends on the template type argument-list, then defining a stand-alone function template wouldn’t solve the problem, because now there will be more than one type argument to the function template, and you cannot partially specialize the function for just, say, one type (as its not allowed).

    So in this case you can define a class template instead, which defines a static non-template member function doTheActualSomething. Here is how:

    template<typename A, typename B>
    struct Worker
    {
       B doTheActualSomething(Thing<A,B> *thing)
       {
          return thing->b_;
       }
    };
    
    //partial specialization of the class template itself, for B = int
    template<typename A>
    struct Worker<A,int>
    {
       int doTheActualSomething(Thing<A,int> *thing)
       {
          return thing->b_ + 1;
       }
    };
    

    Notice that you can use thing pointer to access any member of the class. Of course, if it needs to access private members, then you’ve to make struct Worker a friend of Thing class template, as:

    //forward class template declaration
    template<typename T, typename U> struct Worker
    
    template <class A, class B>
    class Thing
    {
        template<typename T, typename U>  friend struct Worker; //make it friend
       //...
    };
    

    Now delegate the work to the friend as:

    template <class A, class B>
    B Thing<A,B>::doSomething()
    {
        return Worker<A,B>::doTheActualSomething(this); //delegate work
    }
    

    Two points to be noted here:

    • In this solution, doTheActualSomething is not a member function template. Its not enclosing class which is template. Hence we can partially specialize the class template anytime, to get the desired effect of the partial member function template specialization.
    • Since we pass this pointer as argument to the function, we can access any member of the class Thing<A,B>, even private members, as Worker<T,U> is also a friend.

    Complete online demo : http://www.ideone.com/uEQ4S


    Now there is still a chance of improvement. Now all instantiations of Worker class template are friends of all instantiation of Thing class template. So we can restrict this many-to-many friendship as:

    template <class A, class B>
    class Thing
    {
        friend struct Worker<A,B>; //make it friend
       //...
    };
    

    Now only one instantiation of Worker class template is a friend of one instantiation of Thing class template. That is one-to-one friendship. That is, Worker<A,B> is a friend of Thing<A,B>. Worker<A,B> is NOT a friend of Thing<A,C>.

    This change requires us to write the code in somewhat different order. See the complete demo, with all the ordering of class and function definitions and all:

    http://www.ideone.com/6a1Ih

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: I've asked this question in a similiar format on superuser but it seems
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
Note: this is a different problem to https - it's related to privacy security
Note: This is the opposite direction to most similar questions! I have an iPhone
NOTE: This is a solution for Project Euler Problem 14 . If you still
I have an ActiveRecord model class Foo that has_many Bar . I want to
this is a solution for the subset sum problem. It uses backtracking. I have
This is a repost from the msdn forums that got no good results. In
Consider this simple class that demonstrates RAII in C++ (From the top of my
note: all this is on chrome I am trying to test something that responds

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.