相关疑难解决方法(0)

在定义重载的C++函数模板的原型时,使用其名称来引用先前的定义是否合法?

假设我们想要重载函数模板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)

c++ language-lawyer c++11

5
推荐指数
1
解决办法
244
查看次数

标签 统计

c++ ×1

c++11 ×1

language-lawyer ×1