22

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_);
}
Enlico
  • 23,259
  • 6
  • 48
  • 102
Dan
  • 1,258
  • 1
  • 10
  • 22

2 Answers2

54

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

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Good link. I read in your quotation "A class template specialization is a distinct template.". Does this mean that my class specialization will have to define all the expected member data of the original `Thing`? As I read it, the answer is yes... – Dan May 26 '11 at 12:40
  • An interesting bypass of the problem of having to redefine everything is also listed in the link you sent... see my use of `foo` above, as adapted from Alf P. Steinbach's answer. – Dan May 26 '11 at 12:49
  • 2
    The answer is wrong: *Specialization of member function without specializing the class template, is not allowed* is **false**. You cannot *partially* specialize a member function in any context but you can specialize a member function of a class template. `template struct test { void foo() {} }; template <> void test::foo() { std::cout << "specialized"; }` is perfectly valid. – David Rodríguez - dribeas May 26 '11 at 13:06
  • 1
    The workaround is also incorrect: you **cannot** partially specialize a function template. – David Rodríguez - dribeas May 26 '11 at 13:09
  • @David: You're correct regarding the partial specialization. I meant the same, but somehow I carelessly wrote my post and didn't re-read it till you pointed out. And in fact my answer in the other link is correct, and it *emphasizes* what you said. – Nawaz May 26 '11 at 13:18
  • 1
    It is still incorrect: you *cannot* partially specialize a member function. You can implement a generic (non-specialized) member function of a partial specialization, but you cannot partially specialize a member (or non-member) function *ever*. – David Rodríguez - dribeas May 26 '11 at 13:23
  • @David: I didn't partially specialize a member function – Nawaz May 26 '11 at 13:35
  • @David: Ohh.. I understand now what you meant. If I partially specialize a template as `X`, then I can implement `X::f()` which is generic for this partial specialization of the class template, but I cannot implement `X::f()`. That is true. But I didn't do that. – Nawaz May 26 '11 at 13:39
  • It is just that the first sentence is misleading: *Partial specialization of member function without partially specializing the class template, is not allowed.* **No** partial specialization of a member (or free) function is allowed *anywhere*. The sentence seems to indicate that once you have partially specialized the class, then you can *partially* specialize the function, it is just a nitpick, but I believe that in technical descriptions it is important to be precise. – David Rodríguez - dribeas May 26 '11 at 13:52
  • @David: I think, I should delete this post, and add another one (following your pattern of course), explaining the misleading sentences. But then I cannot delete an "accepted" answer. :| – Nawaz May 26 '11 at 13:55
  • @David: Yeah, to be precise is important. But I didn't know one can go to this extent. haha... – Nawaz May 26 '11 at 13:56
  • @Dan: Yes. I will edit it after some time (30 mins or so), with another solution I've in my mind. – Nawaz May 26 '11 at 14:52
  • @David: Please see my post. Do let me know if there is any chance of improvement. Your comments are valueable for me. Maybe, I can still improve it. – Nawaz May 26 '11 at 16:41
  • @Dan: See the post. I edited it extensively (as now I'm at home, not in office :D). – Nawaz May 26 '11 at 16:42
  • Mostly right, the friendship could (probably should) be more explicit (i.e. you don't want to befriend all instantiations of the template `Worker` but rather only `Worker`... but not a big deal. – David Rodríguez - dribeas May 26 '11 at 22:55
  • @David: Yes. I've added that part also. See the last paragraph. – Nawaz May 26 '11 at 23:53
11

This is a very often found problem, and there is a surprisingly simple solution. I will show it in an artificial example, because it's more clearer than to use your code, and you will have to understand it to adapt it to your code

template<typename A, typename B>
struct TwoTypes { };

template<typename A, typename B>
struct X {
  /* forwards ... */
  void f() { fImpl(TwoTypes<A, B>()); }

  /* special overload for <A, int> */
  template<typename A1>
  void fImpl(TwoTypes<A1, int>) {
    /* ... */
  }

  /* generic */
  template<typename A1, typename B1>
  void fImpl(TwoTypes<A1, B1>) {
    /* ... */
  }
};

Explicitly specializing functions is never (almost never?) the right way. In my work as a programmer, I've never explicitly specialized a function template. Overloading and partial ordering is superior.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • The last sentence is so very true, I wish more programmer would knew its value. I remember an article on GotW, that extensively explains the problem with specialized function templates, I can't look for it right now though. – Xeo May 26 '11 at 18:06
  • Also, the overloads could be free functions and you could just pass `this` instead of a new type if you don't need direct access on to the class internals. On a side note, with C++0x one can make a generic `template struct OrderingKey{};` instead of a number of additional structs for each number of types. – Xeo May 26 '11 at 18:12