Difference between decltype (…, void()) and void_t

对着背影说爱祢 提交于 2019-12-10 14:40:40

问题


Last time I'm founding many answers regarding SFINAE which suggest using 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?


回答1:


It's a more expressive, less cumbersome way of saying the same thing.

That's it.



来源:https://stackoverflow.com/questions/35986886/difference-between-decltype-void-and-void-t

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