There are many different tricks for checking whether a class Foo
has a method named bar
. For example:
std::void_t<decltype(static_cast<ing(Foo::*)(char) const>(&Foo::bar))>
std::void_t<decltype(&Foo::bar)>
If Foo
has more than one method named bar
, the former continues to work just fine (since at most one overload will be compatible with the static_cast
), however, the latter will break, presumably because decltype(&Foo::bar)
becomes ambiguous.
Is there a technique that can detect whether a class has one or more methods with a given name, regardless of the method signature(s)? Bonus points if it can be made to work in C++14.
Example Code (Godbolt link):
#include <type_traits>
template<typename T, typename = void> struct HasIntBar_t : std::false_type {};
template<typename T> struct HasIntBar_t<T, std::void_t<decltype(static_cast<int(T::*)(char)>(&T::bar))>> : std::true_type {};
template<typename T> inline constexpr bool HasIntBar = HasIntBar_t<T>::value;
template<typename T, typename = void> struct HasAnyBar_t : std::false_type {};
template<typename T> struct HasAnyBar_t<T, std::void_t<decltype(&T::bar)>> : std::true_type {};
template<typename T> inline constexpr bool HasAnyBar = HasAnyBar_t<T>::value;
////////////////////////////////////
// Test Types:
struct None {};
struct OneInt
{
int bar(char);
};
struct OneIntInherited : public OneInt {};
struct OneDouble
{
double bar() const;
};
struct OneDoubleInherited : public OneDouble {};
struct TwoDirect
{
int bar(char);
double bar() const;
};
struct TwoInherited : public OneInt, public OneDouble
{
using OneInt::bar; // Required to avoid ambiguity
using OneDouble::bar; // Required to avoid ambiguity
};
struct OneInheritedOneDirect : public OneInt
{
using OneInt::bar; // Required to avoid hiding
double bar() const;
};
struct OneInheritedOneDirect2 : public OneDouble
{
using OneDouble::bar; // Required to avoid hiding
int bar(char);
};
////////////////////////////////////
// Tests:
static_assert(HasIntBar<None> == false);
static_assert(HasIntBar<OneInt> == true);
static_assert(HasIntBar<OneIntInherited> == true);
static_assert(HasIntBar<OneDouble> == false);
static_assert(HasIntBar<OneDoubleInherited> == false);
static_assert(HasIntBar<TwoDirect> == true);
static_assert(HasIntBar<TwoInherited> == true);
static_assert(HasIntBar<OneInheritedOneDirect> == true);
static_assert(HasIntBar<OneInheritedOneDirect2> == true);
static_assert(HasAnyBar<None> == false);
static_assert(HasAnyBar<OneInt> == true);
static_assert(HasAnyBar<OneIntInherited> == true);
static_assert(HasAnyBar<OneDouble> == true);
static_assert(HasAnyBar<OneDoubleInherited> == true);
static_assert(HasAnyBar<TwoDirect> == true); // FAILS!
static_assert(HasAnyBar<TwoInherited> == true); // FAILS!
static_assert(HasAnyBar<OneInheritedOneDirect> == true); // FAILS!
static_assert(HasAnyBar<OneInheritedOneDirect2> == true); // FAILS!
You can take advantage of name lookup being ambiguous if it is found in more than one base. First make a struct that derives from your type T
and some dummy type with a single member bar
, then check if that derived type can find bar
. If it can, it wasn't ambiguous since it was found in derived and wasn't in T
, so T
doesn't have a bar. Otherwise T
has a bar
:
struct dummy_bar { int bar; };
template<typename T, bool IsClass = std::is_class<T>::value>
struct HasAnyBar_t {
static_assert(!std::is_final<T>::value, "Cannot check HasAnyBar<T> on final classes for implementation reasons");
template<typename U>
static auto check(int) -> decltype(U::bar, void(), std::false_type{});
template<typename U>
static std::true_type check(long); // Above was ambiguous, so bar did exist in T
struct derived : T, dummy_bar {};
static constexpr bool value = decltype(check<derived>(0))::value;
};
template<typename T>
struct HasAnyBar_t<T, false> : std::false_type {};
template<typename T>
constexpr bool HasAnyBar = HasAnyBar_t<T>::value;
This checks for anything with the name bar
: function overload sets with templates, static data members, non-static data members and member types.