vendredi 4 août 2017

Is `void_t` necessary to check if a class has a method with a specific signature?

When I first learned how to check a specific signature in a class, I was taught to use std::void_t and write some code like this:

template<typename T, typename =void>
class HAS:public false_type{};

template<typename T>
class HAS<T,void_t<decltype(declval<T>().print())>>:public true_type{};

And this snippet of code will check if a class has the method named "print()". It works well.

But when I tried to remove the std::void_t, it still worked.

The code looks like this:

template<typename T, typename = void>
class HAS:public false_type{};

template<typename T>
class HAS<T,decltype(declval<T>().print())>:public true_type{};

So I am confused if "std::void_t" is necessary to check if a class has a method with a specific signature? Or that's only a coincidence? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire