[英]Difference between decltype (…, void()) and void_t
Last time I'm founding many answers regarding SFINAE which suggest using void_t
helper. 上次我建立了很多关于SFINAE的答案,建议使用
void_t
helper。
But I don't seem to understand what's so special about it in regard to: 但我似乎并不明白它在以下方面有什么特别之处:
decltype (..., void()).
Consider the example: 考虑这个例子:
template <typename...>
using void_t = void;
template <typename T, typename = void>
struct has_foo : std::false_type {};
template <typename T>
struct has_foo <T, decltype (T().foo(), void())> : std::true_type {};
template <typename T, typename = void>
struct has_bar : std::false_type {};
template <typename T>
struct has_bar <T, void_t <decltype (T().bar())> > : std::true_type {};
class MyClass1
{
public:
int foo() { return 3; }
};
class MyClass2
{
public:
double bar() { return 5.4; }
};
int main() {
std::cout << has_foo<MyClass1>::value << std::endl;
std::cout << has_foo<MyClass2>::value << std::endl;
std::cout << has_bar<MyClass1>::value << std::endl;
std::cout << has_bar<MyClass2>::value << std::endl;
return 0;
}
The output is as expected for both traits, which makes me think that both implementations are the same. 两个特征的输出都是预期的,这让我觉得两种实现都是一样的。 Am I missing something?
我错过了什么吗?
It's a more expressive, less cumbersome way of saying the same thing. 这是一种更具表现力,不那么繁琐的说法。
That's it. 而已。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.