简体   繁体   English

std::enable_if_t = 0 是什么意思

[英]What's the meaning of std::enable_if_t = 0

I was reading std::enable_if , and noticed the following.我正在阅读std::enable_if ,并注意到以下内容。

template <typename Integer,
          std::enable_if_t<std::is_integral<Integer>::value, int> = 0
>
T(Integer) : m_type(int_t) {}

Since std::enable_if_t is a type, and it can be evaluated to int or void for this case, so the above code can be evaluated to由于 std::enable_if_t 是一个类型,在这种情况下它可以被评估为intvoid ,所以上面的代码可以被评估为

template <typename Integer,
          int = 0
>
T(Integer) : m_type(int_t) {}

or或者

template <typename Integer,
          void = 0
>
T(Integer) : m_type(int_t) {}

I can't understand int = 0 or void = 0 , would someone help me with this?我无法理解int = 0void = 0 ,有人能帮我吗? Thanks.谢谢。

Since std::enable_if_t is a type, and it can be evaluated to int or void for this case, so the above code can be evaluated to ...由于std::enable_if_t是一个类型,在这种情况下它可以被评估为intvoid ,所以上面的代码可以被评估为 ...

This is not correct.这是不正确的。 With std::enable_if_t<std::is_integral<Integer>::value, int> the only type std::enable_if_t can be is int .使用std::enable_if_t<std::is_integral<Integer>::value, int>std::enable_if_t的唯一类型是int If std::is_integral<Integer>::value is not true, then there is no member at all and the template instantiation is thrown out.如果std::is_integral<Integer>::value不为真,则根本没有成员并且模板实例化被抛出。 That means it only resolves to这意味着它只能解析为

template <typename Integer,
          int = 0
>
T(Integer) : m_type(int_t) {}

where int = 0 is just an unnamed non-type template parameter with the value of 0 .其中int = 0只是一个未命名的非类型模板参数,其值为0

The reason we do = 0 is so that it has a default value and we don't need to pass a value to it when declaring a object of this type.我们 do = 0的原因是它有一个默认值,我们不需要在声明这种类型的对象时传递值给它。 Without it you would not be able to use this constructor at all as there is no way to specify the template parameters of a constructor.没有它,您根本无法使用此构造函数,因为无法指定构造函数的模板参数。 Even if this wasn't a constructor, you would still want to use a default value so the user does not need to pass a unneeded value to that unnamed and unused template parmater.即使这不是构造函数,您仍然希望使用默认值,这样用户就不需要将不需要的值传递给未命名和未使用的模板参数。

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

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