键入traits以检查类是否具有成员函数

nCo*_*der 7 c++ type-traits c++14

尝试创建一种方法来识别给定的类是否具有可以调用的给定函数,并返回某种类型.

我在这里做错了什么?有没有更好的方法来确定给定的方法是否可以在给定类的情况下调用?

#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;
}
Run Code Online (Sandbox Code Playgroud)

这个实现有些惊喜.

编辑:试图实施@ Jarod42和@Vittorio Romeo令人敬畏的建议:

#define GENERATE_HAS_MEMBER_FUNC(func, rettype)                                \
template<class T> using _has_##func_chk =                                      \
      decltype(std::declval<T &>().func());                                    \
template<class T> constexpr bool has_##func##_v =                             \
      is_detected_exact_v<rettype, _has_##func_chk, T>;
Run Code Online (Sandbox Code Playgroud)

现在两个测试用例在VS2015上仍然失败(没有任何意义):static_assert(!has_really_v,"A1 :: A1中真的不存在"); static_assert(!has_str2_v,"A2 :: str2在A2中不存在");

可能有些愚蠢的东西我错过了......任何线索?

Vit*_*meo 12

有没有更好的方法来确定给定的方法是否可以在给定类的情况下调用?

是的,您可以使用检测习惯用法,该习惯用法可以在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>);
Run Code Online (Sandbox Code Playgroud)

wandbox示例


在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;
};
Run Code Online (Sandbox Code Playgroud)

这是wandbox上完全符合C++ 11标准的最小示例.