5

This is my first attempt at SFINAE:

#include <type_traits>
#include <iostream>

struct C1 {
    using T = int;
};

struct C2 {
    using T = void;
};

// For classes that declare T = int
template <class C>
void f(C &c,
       std::enable_if<!std::is_same<typename C::T, void>::value, int>::type = 0) {
    std::cout << "With T" << std::endl;
}

// For classes that declare T = void
template <class C>
void f(C &c,
       std::enable_if<std::is_same<typename C::T, void>::value, int>::type = 0) {
    std::cout << "Without T" << std::endl;
}

int main() {
    C1 c1;
    f(c1); // With T
    C2 c2;
    f(c2); // Without T
    return 0;
}

The compiler (gcc 4.8.2) complains:

‘std::enable_if<!(std::is_same<typename C::T, void>::value), int>::type’ is not a type 

What am I doing wrong?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
  • 2
    You need to write `typename std::enable_if<...>::type` because `type` is dependent. [Read this](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords). If you can use C++14 you can use `std::enable_if_t<...>` instead and you won't have this problem. – Simple Dec 10 '15 at 15:27
  • the best would be to place `enable_if`s in template parameters to not clutter function arguments – Guillaume Racicot Dec 10 '15 at 15:41
  • I made an edit to your code. I presume these were things that you just omitted or typed incorrectly, but I added them for clarity. – erip Dec 10 '15 at 17:33
  • 1
    Further, is there any reason you didn't use `is_void` and `is_integral`? Based on your username, I assume these are just learning exercises. :) – erip Dec 10 '15 at 17:41
  • @erip Based on my user name, I did not use them because I haven't learnt them yet :-) Thank you for mentioning them. – AlwaysLearning Dec 11 '15 at 07:27

1 Answers1

7

You need a couple of typenames for this to work:

// For classes that declare T = int
template <class C>
void f(C &c,
       typename std::enable_if<!std::is_same<typename C::T, void>::value, int>::type = 0) {
    std::cout << "With T" << std::endl;
}

// For classes that declare T = void
template <class C>
void f(C &c,
       typename std::enable_if<std::is_same<typename C::T, void>::value, int>::type = 0) {
    std::cout << "Without T" << std::endl;
}

Or if your compiler supports C++14 you can use std::enable_if_t:

// For classes that declare T = int
template <class C>
void f(C &c,
       std::enable_if_t<!std::is_same<typename C::T, void>::value, int> = 0) {
    std::cout << "With T" << std::endl;
}

// For classes that declare T = void
template <class C>
void f(C &c,
       std::enable_if_t<std::is_same<typename C::T, void>::value, int> = 0) {
    std::cout << "Without T" << std::endl;
}
101010
  • 41,839
  • 11
  • 94
  • 168