Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking whether a type declaration is present for SFINAE

I would like to use SFINAE to overload a function template based on whether the template argument declares a type T. This is where I was able to get so far:

struct C1 {
    using T = int;
};

struct C2 {
    using T = void; // but I would really like 
                    // to not require T at all
};

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

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

int main() {
    C2 c;
    f(c);
    return 0;
}

How (if at all) is it possible to change this solution in a way that C2 would not need to declare T at all? I.e. I would like to have two overloads: one for classes that declare T and one for classes that do not.

like image 642
AlwaysLearning Avatar asked Feb 09 '23 08:02

AlwaysLearning


2 Answers

#include <type_traits>

template <typename...>
struct voider { using type = void; };

template <typename... Ts>
using void_t = typename voider<Ts...>::type;

template <typename C, typename = void_t<>>
struct has_t : std::false_type {};

template <typename C>
struct has_t<C, void_t<typename C::T>> : std::true_type {};

template <typename C>
auto f(C& c)
    -> typename std::enable_if<has_t<C>::value>::type
{
}

template <typename C>
auto f(C& c)
    -> typename std::enable_if<!has_t<C>::value>::type
{
}

DEMO

like image 81
Piotr Skotnicki Avatar answered Feb 16 '23 03:02

Piotr Skotnicki


You can make a type trait to suit your needs:

template<typename Type, typename Enable=void>
struct has_t : std::false_type {};

template<typename Type>
struct has_t<Type, void_t<Type::T>> : std::true_type {};

void_t is implemented as follow:

template<typename...>
struct voider {
    using type = void;
};

template<class... Ts> using void_t = typename voider<Ts...>::type;

This void_t implementation is guaranteed to work in C++11.

Now you can use this type trait like this:

template<typename C, typename std::enable_if<has_t<typename std::decay<C>::type>::value, int>::type = 0>
void f(C&& c) {
    // ... C::T exists
}

template<typename C, typename std::enable_if<!has_t<typename std::decay<C>::type>::value, int>::type = 0>
void f(C&& c) {
    // ... C::T doesn't exists
}

You need to decay the type as it will be a reference. Since the typedef inside C is not inside, let's say C&, you must remove the reference first.

like image 40
Guillaume Racicot Avatar answered Feb 16 '23 02:02

Guillaume Racicot