假设我们想要重载函数模板f,但仅在尚未声明类似的重载时:
template<typename T>
void f(T); // main prototype
struct A {};
struct B {};
//we want to declare B f(A), but only if something like A f(A) hasn't been declared
//we can try to check the type of expression f(A) before defining it
//and disable overload via enable_if
template<typename T = void> //it has to be a template to use enable_if
std::enable_if_t<std::is_same_v<void, decltype(T(), (f)(A{}))>, B> f(A);
// decltype expression depends on T, but always evaluates to the type …Run Code Online (Sandbox Code Playgroud)