W jaki sposób użyć std :: enable_if z typem powrotu do samodzielnego obliczania?

C ++ 14 będą miały funkcje, których typ powrotu można wywnioskować na podstawie wartości zwracanej.

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

Czy mogę zastosować to zachowanie do funkcji, które używająenable_if dla SFINAE idiomem typu zwrotnego?

Na przykład rozważmy następujące dwie funkcje:

#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"

}

Jak widzisz, poprawna funkcja jest wybierana za pomocą idiomu typu SFINAE przez zwrot. Są to jednak obie funkcje puste. Drugi parametrenable_if jest domyślnie ustawione navoid. To byłoby takie samo:

//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;
}

Czy jest coś, co mogę zrobić z tymi dwiema funkcjami, aby ich typ zwracany był przez wartość zwracaną?

gcc 4.8.2 (przy użyciu--std=c++1y)

questionAnswers(4)

yourAnswerToTheQuestion