1

In this question:

Check if a class has a member function of a given signature

They address the problem of how to determine if a class has a member function of some type. This could sound as a naive question but I couldn't find out on my own. How can I determine if there is a static function of that given signature? Could you extend the example given in the question linked to determine if there is a static function in that class that returns used_memory or whatever?

Community
  • 1
  • 1
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114

1 Answers1

0

You may use the following:

#include <cstdint>

#define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature)               \
    template <typename U>                                                   \
    class traitsName                                                        \
    {                                                                       \
    private:                                                                \
        template<typename T, T> struct helper;                              \
        template<typename T>                                                \
        static std::uint8_t check(helper<signature, &funcName>*);           \
        template<typename T> static std::uint16_t check(...);               \
    public:                                                                 \
        static                                                              \
        constexpr bool value = sizeof(check<U>(0)) == sizeof(std::uint8_t); \
    }

// signature is the important part for static. it is not (T::*)
DEFINE_HAS_SIGNATURE(has_static_foo, T::foo, void (*)(void));

// signature use (T::*) here for member
DEFINE_HAS_SIGNATURE(has_member_foo, T::foo, void (T::*)(void));

Live Example

Jarod42
  • 203,559
  • 14
  • 181
  • 302