I'm trying to find a way to simply check if a method of a given name exists in a c++ class using c++11 features, but without(!) checking the signature.
I was unable to find anything without the signature check, so I tried to use Valentin Milea's solution from here and modify it (see below), but my understanding of c++ is not deep enough to really grasp what's going on there:
#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 as base class
bool approx_equal(const Base& other, double tolerance) const;
};
class Other {};
static_assert(HasApproxEqualMethod<Base>().value == true, "fail Base");
static_assert(HasApproxEqualMethod<Other>().value == false, "fail Other");
// this one fails:
static_assert(HasApproxEqualMethod<Derived>().value == true, "fail Derived");
I think the root of the problem is that my approx_equal
uses base class references also in the derived class(es) and that doesn't match the signature anymore.
In the end, I want to construct a template comparison function that calls approx_equal
if it exists, or something else( e.g. ==
for strings etc., or fabs(a-b) <= tolerance
for floats and doubles). The approx_equal functions will then in turn call the template comparison for each member.
I actually got that part to work with an ugly workaround, where each class with an approx_equal
method also gets a member variable const static char hasApproxEqualMethod
. Then I check if that variable exists as suggested here, but that's certainly not the way to go.
operator()
withapprox_equal
. – Veator