Coming from this question, how can we detect (at compile time) if a function/operator/method having certain signature is defined?
From linked question and looking at cppreference about std::void_t we can write (C++17 ahead)
#include<iostream>
#include<type_traits>
#include<utility>
class X { public: int someFunc() const{ return 9; } };
class Y{};
template<typename, typename = std::void_t<>> struct Has_someFunc
: std::false_type{};
template<typename T> struct Has_someFunc<T, std::void_t<decltype(std::declval<T>().someFunc())>>
: std::true_type{};
template<typename T>
void f(const T& v){
if constexpr(Has_someFunc<T>::value)
std::cout << "has someFunc()\n";
else
std::cout << "has NOT someFunc()\n";
}
int main()
{
std::cout << "X "; f(X{});
std::cout << "Y "; f(Y{});
return 0;
}
which prints
X has someFunc()
Y has NOT someFunc()
but what if we want to test a type to have someFunc
not only to be defined but also having a certain signature?
Though I'm using C++17 also answers in any other version of the standard are welcome.