How do I use std::enable_if with a self-deducing return type?

Trevor Hickey picture Trevor Hickey · Dec 20, 2013 · Viewed 8.3k times · Source

C++14 will have functions whose return type can be deduced based on the return value.

auto function(){
    return "hello world";
}

Can I apply this behaviour to functions that use enable_if for the SFINAE by return type idiom?

For example, let's consider the following two functons:

#include <type_traits>
#include <iostream>

//This function is chosen when an integral type is passed in
template<class T >
auto function(T t) -> typename std::enable_if<std::is_integral<T>::value>::type {
    std::cout << "integral" << std::endl;
    return;
}

//This function is chosen when a floating point type is passed in
template<class T >
auto function(T t) -> typename std::enable_if<std::is_floating_point<T>::value>::type{
    std::cout << "floating" << std::endl;
    return;
}

int main(){

  function(1);    //prints "integral"
  function(3.14); //prints "floating"

}

As you can see, the correct function is chosen using the SFINAE by return type idiom. However, these are both void functions. The second parameter of enable_if is default set to void. This would be the same:

//This function is chosen when an integral type is passed in
template<class T >
auto function(T t) -> typename std::enable_if<std::is_integral<T>::value, void>::type {
    std::cout << "integral" << std::endl;
    return;
}

//This function is chosen when a floating point type is passed in
template<class T >
auto function(T t) -> typename std::enable_if<std::is_floating_point<T>::value, void>::type{
    std::cout << "floating" << std::endl;
    return;
}

Is there something I can do to these two functions, so that their return type is deduced by the return value?

gcc 4.8.2 (using --std=c++1y)

Answer

Rapptz picture Rapptz · Dec 20, 2013

std::enable_if doesn't have to be in the return type, as of C++11 it can be part of the template parameters.

So your equivalent functions can be (or, well, something to this effect):

enum class enabler_t {};

template<typename T>
using EnableIf = typename std::enable_if<T::value, enabler_t>::type;

//This function is chosen when an integral type is passed in
template<class T, EnableIf<std::is_integral<T>>...>
auto function(T t) {
    std::cout << "integral" << std::endl;
    return;
}

//This function is chosen when a floating point type is passed in
template<class T, EnableIf<std::is_floating_point<T>>...>
auto function(T t) {
    std::cout << "floating" << std::endl;
    return;
}

It can also be a parameter in the function:

//This function is chosen when an integral type is passed in
template<class T>
auto function(T t, EnableIf<std::is_integral<T>>* = nullptr) {
    std::cout << "integral" << std::endl;
    return;
}

//This function is chosen when a floating point type is passed in
template<class T>
auto function(T t, EnableIf<std::is_floating_point<T>>* = nullptr) {
    std::cout << "floating" << std::endl;
    return;
}

This will keep the automatic type deduction and SFINAE.