【发布时间】:2019-04-23 21:27:52
【问题描述】:
例如,我写了如下一段代码:
struct default_type {};
template <typename U = default_type> auto func(int x, U u) {
if constexpr(is_same<U, default_type>::value) {
return x * 2;
}
else {
return u(x);
}
}
int main() {
cout << "5 + 3 = " << func(5, [](int x){ return x + 3; }) << endl;
cout << "5 * 2 = " << func(5) << endl;
}
但这会产生“没有匹配函数调用'func(int)'”的编译器错误。这样做的正确方法是什么?
【问题讨论】:
标签: c++