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
)
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):
It can also be a parameter in the function:
This will keep the automatic type deduction and SFINAE.
As mentioned elsewhere
std::enable_if
can be used to form either a return type; a function parameter; or a template parameter.However, the latter two methods have the disdvantage that they change the signature of the relevant function or object. Using
std::enable_if
at the return type, on the other hand, leaves the function and template parameter count unchanged.With the automatic inference of lambda return types in C++11 (likely extended to normal functions in C++14) it would be ideal if there was a technique to both have the return type inferred; and use
std::enable_if
on the return type. To have your cake an eat it - almost. Alas it seems that this is currently impossible.std::enable_if
can be a return type, a function parameter, or a template parameter. You will get a function redefinition error if you use return type or template parameter, so you need to usestd::enable_if
as a function parameter:In the line of the answer by @user1508519, we can remove the enable_if parameter from the method and keep it only as a template parameter. We rely on the fact that
enable_if<false>
doesn't definetype
, soenable_if<false>::type
, which doesn't exist, is a good tool for SFINAE -- in the method signature, which includes the template parameters.There is no need to actually use this template parameter in the method itself!
Thus:
Output