简体   繁体   English

SFINAE: std::enable_if 作为函数参数

[英]SFINAE: std::enable_if as function argument

So, I'm following the example set by the code somewhere on this web page: http://eli.thegreenplace.net/2014/sfinae-and-enable_if/因此,我正在按照此网页某处的代码设置的示例进行操作: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 .编译器抱怨它无法推断出参数T

The examples are wrong, since T is in a non-deduced context .这些示例错误的,因为T处于非推导上下文中 Unless you call the function like fun<int>(4);除非你像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, eg, in a return type syntax:正确的用法是允许编译器推导T ,并将 SFINAE 条件放在其他地方,例如,在返回类型语法中:

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 typename s in your code contradict your usage of std::enable_if_t .此外,代码中的typename与您对std::enable_if_t的使用相矛盾。

Use either :使用任一

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

or :

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:对于构造函数,SFINAE 条件可以隐藏在模板参数列表中:

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

Alternatively, in , you can use concepts for that:或者,在中,您可以为此使用概念:

A(const std::integral auto& val);

A(const std::floating_point auto& val);

To allow deduction you need a function parameter that is straightforwardly based on T .要进行推导,您需要一个直接基于T的函数参数。 You then need to figure out where to put your enable_if (which indeed does not allow T to be deduced).然后,您需要弄清楚将enable_if放在哪里(这确实不允许推导T )。 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这里有一些很好的例子:http: //en.cppreference.com/w/cpp/types/enable_if

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM