13

Is it possible to do something like the following that compiles without template specialization?

template <class T> 
class A {
public:
    #if std::is_same<T, int>
        void has_int() { }
    #elif std::is_same<T, char>
        void has_char() { }
    #endif
};
A<int> a; a.has_int();
A<char> b; b.has_char();
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
ZeroCool
  • 1,490
  • 4
  • 19
  • 33

2 Answers2

24

Yes. Make the function templates and then conditionaly enable them using std::enable_if:

#include <type_traits>

template <class T> 
class A {
public:

  template<typename U = T>
  typename std::enable_if<std::is_same<U,int>::value>::type
  has_int() {}

  template<typename U = T>
  typename std::enable_if<std::is_same<U,char>::value>::type
  has_char() {}
};

int main()
{
    A<int> a;
    a.has_int();   // OK
    // a.has_char();  // error
}

The solution from the other answer might not be feasible if the class is big and has got many functions that need to regardless of T. But you can solve this by inheriting from another class that is used only for these special methods. Then, you can specialize that base class only.

In C++14, there are convenient type aliases so the syntax can become:

std::enable_if_t<std::is_same<U, int>::value>

And C++17, even shorter:

std::enable_if_t<std::is_same_v<U, int>>
Community
  • 1
  • 1
jrok
  • 54,456
  • 9
  • 109
  • 141
  • 3
    you could add that in C++14 you can write `std:::enable_if_t` and in C++17 even `std::is_same_v` in order to get rid of the `typename ... ::type` and `::value` verbosity. – TemplateRex Dec 16 '14 at 13:15
6

Yes, with template specialization :

template <class T> 
class A;

template <> 
class A<int>
{
    void had_int(){}
};

template <> 
class A<char>
{
    void had_char(){}
};
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • Is there any way without rewriting the same code twice? – ZeroCool Dec 16 '14 at 12:09
  • You can introduce new class member declarations with template specialization? That's something new for me. – πάντα ῥεῖ Dec 16 '14 at 12:11
  • 1
    @ZeroCool Maybe in the future you can: http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Static-If-I-Had-a-Hammer – 5gon12eder Dec 16 '14 at 12:12
  • @ZeroCool It is not the same code twice. You are implementing different behavior when a template argument is different. – BЈовић Dec 16 '14 at 12:27
  • @BЈовић But presumably those classes have more members that would be the same. – Sebastian Redl Dec 16 '14 at 12:28
  • 2
    @5gon12eder Static if was rejected in the committee. Haven't heard of any effort to bring it back. – Sebastian Redl Dec 16 '14 at 12:29
  • @SebastianRedl I don't see more members in the question. Anyway, you could add another class that uses this template. – BЈовић Dec 16 '14 at 12:31
  • @BЈовић Because it's a reduced example. If he had posted the code with additional members, people would have complained about a non-minimal example. And yes, you can do class splits to work around this issue, but it's a workaround and can get rather complicated. – Sebastian Redl Dec 16 '14 at 12:32
  • @BЈовић Thanks for proposal. Actually original class is enormous which I agree is not good design anyway. Splitting it and template specializing it would create more code than it is already needed. – ZeroCool Dec 16 '14 at 12:41
  • @SebastianRedl I didn't know that. My state of knowledge was that it was postponed for later decision. Honestly, I'm not too sad about that decision, I must say… – 5gon12eder Dec 16 '14 at 12:42
  • @πάνταῥεῖ yes, as long as it has the same number of template parameters a specialization can be totally different from the primary template ... although that would usually be a bad idea to do :) – Jonathan Wakely Dec 16 '14 at 12:48