Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get SFINAE to work

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?

like image 609
AlwaysLearning Avatar asked Dec 10 '15 15:12

AlwaysLearning


1 Answers

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;
}
like image 91
101010 Avatar answered Sep 24 '22 00:09

101010