SFINAE: std::enable_if as function argument

丶灬走出姿态 提交于 2019-12-07 00:04:13

问题


So, I'm following the example set by the code somewhere on this web page: http://eli.thegreenplace.net/2014/sfinae-and-enable_if/

Here's what I have:

template<typename T>
void fun(const typename std::enable_if_t<std::is_integral<T>::value, T>& val) {
    std::cout << "fun<int>";
}

template<typename T>
void fun(const typename std::enable_if_t<std::is_floating_point<T>::value, T>& val) {
    std::cout << "fun<float>";
}

int main()
{
    fun(4);
    fun(4.4);
}

This way I would have to write:

fun<int>(4);
fun<double>(4.4);

How would I avoid that?

Compiler complains that it can't deduce the parameter T.


回答1:


The examples are wrong, since T is in a non-deduced context. Unless you call the function like fun<int>(4);, the code won't compile, but this is probably not what the author intended to show.

The correct usage would be to allow T to be deduced by the compiler, and to place a SFINAE condition elsewhere, e.g. in a return type syntax:

template <typename T>
auto fun(const T& val)
    -> typename std::enable_if<std::is_integral<T>::value>::type
{
    std::cout << "fun<int>";
}

template <typename T>
auto fun(const T& val)
    -> typename std::enable_if<std::is_floating_point<T>::value>::type
{
    std::cout << "fun<float>";
}

DEMO

Also, the typenames in your code contradict your usage of std::enable_if_t.

Use either (C++11):

typename std::enable_if<...>::type

or (C++14):

std::enable_if_t<...>

How would that work in a constructor which doesn't have a return type though?

In case of constructors, the SFINAE condition can be hidden in a template parameter list:

struct A
{    
    template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
    A(const T& val)
    {
        std::cout << "A<int>";
    }

    template <typename T, typename std::enable_if<std::is_floating_point<T>::value, int>::type = 0>
    A(const T& val)
    {
        std::cout << "A<float>";
    }
};

DEMO 2




回答2:


To allow deduction you need a function parameter that is straightforwardly based on T. You then need to figure out where to put your enable_if (which indeed does not allow T to be deduced). Common options are on the return type or on an extra default parameter that you ignore.

Some good examples here: http://en.cppreference.com/w/cpp/types/enable_if



来源:https://stackoverflow.com/questions/34369072/sfinae-stdenable-if-as-function-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!