Check if class has method C++17

Hi, I've tried few options, but seems, this is the most compact code for checking if class has some method:

template <typename T>
static constexpr bool hasFunction(...) { return false; }

template <typename T>
static constexpr bool hasFunction(int, decltype((std::declval<T>().myFunc(float() )))* = 0) { return true; }

Check method availability with next constexpr condition:
if constexpr (hasFunction<MyClass>(0)) {
      // Your code here
}

SFINAE above work on VS17, latest Clang and Gcc 6. I tested it only with C++17. But it should also work with C++14.
Hope this help.

Комментарии

Популярные сообщения из этого блога

Siege Up! Editor (beta)

STM32F4 и программный выход в DFU

Git и Yandex.Disk