102

I need to specialize template member function for some type (let's say double). It works fine while class X itself is not a template class, but when I make it template GCC starts giving compile-time errors.

#include <iostream>
#include <cmath>

template <class C> class X
{
public:
   template <class T> void get_as();
};

template <class C>
void X<C>::get_as<double>()
{

}

int main()
{
   X<int> x;
   x.get_as();
}

here is the error message

source.cpp:11:27: error: template-id
  'get_as<double>' in declaration of primary template
source.cpp:11:6: error: prototype for
  'void X<C>::get_as()' does not match any in class 'X<C>'
source.cpp:7:35: error: candidate is:
  template<class C> template<class T> void X::get_as()

What is the problem here, and how can I fix it?

starball
  • 20,030
  • 7
  • 43
  • 238
ledokol
  • 1,031
  • 2
  • 8
  • 5

3 Answers3

123

It doesn't work that way. You would need to say the following, but it is not correct

template <class C> template<>
void X<C>::get_as<double>()
{

}

Explicitly specialized members need their surrounding class templates to be explicitly specialized as well. So you need to say the following, which would only specialize the member for X<int>.

template <> template<>
void X<int>::get_as<double>()
{

}

If you want to keep the surrounding template unspecialized, you have several choices. I prefer overloads

template <class C> class X
{
   template<typename T> struct type { };

public:
   template <class T> void get_as() {
     get_as(type<T>());
   }

private:
   template<typename T> void get_as(type<T>) {

   }

   void get_as(type<double>) {

   }
};
nonsensickle
  • 4,438
  • 2
  • 34
  • 61
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • why do you need the `type<>` wrapper? couldn't a cast of a 0 to a pointer of type `T` do the trick? I guess it's not as elegant... – Nim Apr 01 '11 at 12:08
  • Looks like this is really not possible to do. Thanks. – ledokol Apr 01 '11 at 12:09
  • 4
    @Nim right, I think the pointer cast thing is ugly, and wouldn't work for types you can't form pointers to (references). Also, having a function parameter be a pointer to an array type without a size is illegal in C++. Having it in a type wrapper makes it work for all types. – Johannes Schaub - litb Apr 01 '11 at 12:09
  • 2
    @JohannesSchaub-litb : What are the other choices along overloads ? Can show some of them ? – Jean-Bernard Jansen Feb 18 '14 at 22:12
  • @JohannesSchaub-litb Thanks! I love this answer! It's really elegant and an intriguing way of having it your way without making it too ugly of a hack... Could you just explain to me why the type<> wrapper is needed? I didn't quite get the comment from Nim but I'm so eager to understand what his comment was about and why this solution is better.. Thank you!!! – fast-reflexes Oct 30 '14 at 20:43
  • 2
    @fast-reflexes his comment was to use `template void get_as(T*); void get_as(double*);` and pass a `(T*)0`. – Johannes Schaub - litb Oct 30 '14 at 22:29
  • @JohannesSchaub-litb In the current draft of the standard, I see that only the explicit specializations of **class template** members is prohibited if their enclosing class templates are not specialized as well (http://eel.is/c++draft/temp.expl.spec#17). Is there other paragraph in the standard which prohibits the explicit specialization of all members? Thank you. – user42768 Apr 16 '18 at 18:20
  • @JohannesSchaub-litb I see that others have the same interpretation as yours. Am I reading that paragraph wrong? Thank you. – user42768 Apr 18 '18 at 07:08
  • I did not see the point to do what you suggested right now. What you suggested need to declare the `get_as(type)` in template X. But normally we want to use template is just because we want allow lib users to declare `get_as()` for their own type. If you just want to specilize `get_as()` You can simply do that directly in class X if you are using c++17. – Wang May 26 '21 at 19:54
31

If one is able to used std::enable_if we could rely on SFINAE (substitution failure is not an error)

that would work like so (see LIVE):

#include <iostream>
#include <type_traits>

template <typename C> class X
{
public:
    template <typename T, 
              std::enable_if_t<!std::is_same_v<double,T>, int> = 0> 
    void get_as() { std::cout << "get as T" << std::endl; }

    template <typename T, 
              std::enable_if_t<std::is_same_v<double,T>, int> = 0> 
    void get_as() { std::cout << "get as double" << std::endl; }
};

int main() {
   X<int> d;
   d.get_as<double>();

   return 0;
}

The ugly thing is that, with all these enable_if's only one specialization needs to be available for the compiler otherwise disambiguation error will arise. Thats why the default behaviour "get as T" needs also an enable if.

Gabriel
  • 8,990
  • 6
  • 57
  • 101
21

Probably the cleanest way to do this in C++17 and on-wards is to use a if constexpr in combination with the std::is_same_v type trait without explicitly specialisation at all:

#include <iostream>
#include <type_traits>

template <typename C>
class X {
  public:
    template <typename T> 
    void get_as() { 
      // Implementation part for all types
      std::cout << "get as ";

      // Implementation part for each type separately
      if constexpr (std::is_same_v<double, T>) {
        std::cout << "'double'";
      } else if constexpr (std::is_same_v<int, T>) {
        std::cout << "'int'";
      } else {
        std::cout << "(default)";
      }

      // Implementation part for all types
      std::cout << std::endl;
      return;
    }
};

int main() {
  X<int> d {};
  d.get_as<double>(); // 'double'
  d.get_as<int>();    // 'int'
  d.get_as<float>();  // (default)

  return EXIT_SUCCESS;
}

Try it here!


If you need to have a return type as well you could declare the return type as auto:

template <typename T> 
auto get_as() { 
  if constexpr (std::is_same_v<double, T>) {
    return 0.5;
  } else {
    return 0;
  }
}
2b-t
  • 2,414
  • 1
  • 10
  • 18