Reputation: 118
Consider an overloaded function
void f(int);
void f(long);
void f(double);
void f(MyClass);
And a method in a template class with unknown argument type
template <class T>
struct C {
void method(T arg) { ... }
};
I want to check at compile time if there is a version of f
which can take arg
as an argument.
template <class T>
struct C {
void method(T arg) {
if constexpr (CAN_BE_CALLED(f, arg)) {
f(arg);
} else {
g();
}
}
};
Is it possible to do that? I tried this and this but compiler complained about unresolved overloaded function type.
Upvotes: 9
Views: 2784
Reputation: 21131
You could use the detection idiom to build such a test
template<typename = void, typename... Args>
struct test : std::false_type {};
template<typename... Args>
struct test<std::void_t<decltype(f(std::declval<Args>()...))>, Args...>
: std::true_type {};
template<typename... Args>
inline constexpr bool test_v = test<void, Args...>::value;
And use it as
template <class T>
struct C
{
void method(T arg)
{
if constexpr (test_v<T>)
f(arg);
else
g();
}
};
Or alternatively
template<typename... Args>
using test_t = decltype(f(std::declval<Args>()...));
template<typename... Args>
inline constexpr auto test_v = std::experimental::is_detected_v<test_t, Args...>;
Upvotes: 7