Suppose you have a family of type-unrelated classes implementing a common concept by means of a given method returning a value:
class A { public: int val() const
Use this type trait class to determine whether a type has got val
member function.
template
struct has_val {
typedef char yes;
typedef struct { char c[2]; } no;
template
static constexpr auto test(int) -> decltype( std::declval().val(), yes() );
template
static constexpr no test(...);
static constexpr bool value = sizeof(test(0)) == sizeof(yes);
};
You can use it as a condition to enable_if
.