I was trying to detect the presence of a member function baz()
in a template parameter:
template<typename T, typename = void>
struct ImplementsBaz : public std::false_type { };
template<typename T>
struct ImplementsBaz<T, decltype(&T::baz)> : public std::true_type { };
But it always produces false:
struct Foo {};
struct Bar { void baz() {} };
std::cout << ImplementsBaz<Foo>::value << std::endl; // 0
std::cout << ImplementsBaz<Bar>::value << std::endl; // also 0
Using declval
and calling the method does work, though:
template<typename T>
struct ImplementsBaz<T, decltype(std::declval<T>().baz())> : public std::true_type { };
Of course, now this can only detect a baz
function with 0 arguments. Why is the specialization correctly selected when using declval<T>().baz()
, but not decltype(&T::baz)
?
Aucun commentaire:
Enregistrer un commentaire