尝试创建一种方法来识别给定的类是否具有可以调用的给定函数,并返回某种类型.
我在这里做错了什么?有没有更好的方法来确定给定的方法是否可以在给定类的情况下调用?
#include <string>
#include <type_traits>
#define GENERATE_HAS_MEMBER_FUNC(func, rettype) \
template<typename T, class Enable = void> struct has_##func; \
template<typename T, class U> struct has_##func : std::false_type {}; \
template<typename T> \
struct has_##func<T, \
typename std::enable_if<std::is_same< \
typename std::result_of<decltype (&T::func)(T)>::type, \
rettype>::value>::type> : std::true_type{}; \
template<class T> constexpr bool has_##func##_v = has_##func<T>::value;
GENERATE_HAS_MEMBER_FUNC(str, std::string)
GENERATE_HAS_MEMBER_FUNC(str2, std::string)
GENERATE_HAS_MEMBER_FUNC(funca, std::string)
GENERATE_HAS_MEMBER_FUNC(strK, std::string)
GENERATE_HAS_MEMBER_FUNC(fancy, std::string)
GENERATE_HAS_MEMBER_FUNC(really, std::string)
struct A1 {
virtual std::string str() const { return ""; }
std::string strK() const { …Run Code Online (Sandbox Code Playgroud)