小编Han*_*nno的帖子

检查一个类是否有一个具有给定名称但有任何签名的方法

我试图找到一种方法来简单地检查给定名称的方法是否存在于使用 c++11 特性的 c++ 类中,但没有(!)检查签名。

如果没有签名检查,我无法找到任何东西,所以我尝试从这里使用 Valentin Milea 的解决方案并对其进行修改(见下文),但我对 C++ 的理解不够深入,无法真正掌握那里发生的事情:

#include <type_traits>

template <class C>
class HasApproxEqualMethod
{
    template <class T>
    static std::true_type testSignature(bool (T::*)(const T&, double) const);

    template <class T>
    static decltype(testSignature(&T::approx_equal)) test(std::nullptr_t);

    template <class T>
    static std::false_type test(...);

public:
    using type = decltype(test<C>(nullptr));
    static const bool value = type::value;
};

class Base {
public:
virtual ~Base();
virtual bool approx_equal(const Base& other, double tolerance) const;
};

class Derived : public Base {
public:
     // same interface …
Run Code Online (Sandbox Code Playgroud)

c++ methods templates sfinae c++11

4
推荐指数
2
解决办法
1762
查看次数

标签 统计

c++ ×1

c++11 ×1

methods ×1

sfinae ×1

templates ×1