尝试创建一种方法来识别给定的类是否具有可以调用的给定函数,并返回某种类型。
我在这里做错了什么?有没有更好的方法来确定给定方法是否可以调用给定类?
C
这个实现有些惊喜。
编辑: 在试图实现@ Jarod42和@Vittorio Romeo令人敬畏的建议时:
#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 { return ""; }
virtual std::string fancy()=0;
};
struct A2 : A1 {
std::string str() const override { return ""; }
std::string funca();
std::string fancy() override { return ""; }
std::string really(int a=0) const { return std::to_string(a); }
};
int main() {
static_assert(has_str_v<A1>,
"A1::str is virtual method with impl on base"); // MSVC: NO, clang: OK, GCC: NO
static_assert(has_strK_v<A1>,
"A1::strK is implemented inline "); // MSVC: NO, clang: OK, GCC: NO
static_assert(has_fancy_v<A1>,
"A1::fancy is a pure virtual method on base"); // MSVC: NO, clang: OK, GCC: NO
static_assert(!has_really_v<A1>,
"A1::really doesn't exist in A1"); // MSVC: OK, clang: OK, GCC: OK
static_assert(has_str_v<A2>,
"A2::str is override method "); // MSVC: OK, clang: OK, GCC: OK
static_assert(!has_str2_v<A2>,
"A2::str2 does not exist in A2"); // MSVC: NO, clang: OK, GCC: OK
static_assert(has_funca_v<A2>,
"A2::funca is defined (no impl) in A2"); // MSVC: OK, clang: OK, GCC: OK
static_assert(has_strK_v<A2>,
"A2::strK is implemented method on base"); // MSVC: OK, clang: OK, GCC: OK
static_assert(has_fancy_v<A2>,
"A1::fancy is a override of pure virtual method of base"); // MSVC: OK, clang: OK, GCC: OK
static_assert(has_really_v<A2>,
"A2::really has default param (can be invoked without params)"); // MSVC: OK, clang: NO, GCC: NO
return 0;
}
现在两个测试用例在VS2015上仍然失败(没有任何意义): static_assert(!has_really_v,&#34; A1 ::确实不存在于A1&#34;); static_assert(!has_str2_v,&#34; A2 :: str2在A2&#34中不存在;);
我可能会遗漏一些愚蠢的东西......任何线索?
答案 0 :(得分:12)
有没有更好的方法来确定给定方法在给定类时是否可以调用?
是的,您可以使用detection idiom,它可以在C ++ 11 中实现(链接页面包含有效的实现)。
以下是一个示例:Cat
是否有float Cat::purr(int)
方法?
struct Cat { float purr(int){} };
template<class T>
using has_purr =
decltype(std::declval<T&>().purr(std::declval<int>()));
static_assert(std::experimental::is_detected_exact_v<float, has_purr, Cat>);
在C ++ 11中实现所需的检测习惯 C ++ 17依赖项是微不足道的:
template< class... >
using void_t = void;
struct nonesuch {
nonesuch() = delete;
~nonesuch() = delete;
nonesuch(nonesuch const&) = delete;
void operator=(nonesuch const&) = delete;
};