class Foo {
public:
int Add(int a);
private:
Foo() = delete;
};
template<typename T>
T MyDeclVal_(long) {
// static_assert(false);
}
template<typename T, typename U=T&&> //U = void &&,因为void &&编译失败,按SFINAE,不选这个
U MyDeclVal_(int);
template<typename T>
auto MyDeclVal() -> decltype(MyDeclVal_<T>(0));
decltype(MyDeclVal<void>()) fun(){} //参数是void
int main(int argc, char *argv[]) {
decltype(MyDeclVal<Foo>().Add(3)) x;
x = 4;
LOG(INFO) << "x = " << x;
}
C++20 SFINAE 替换失败并非错误(Substitution Failure Is Not An Error)
最新推荐文章于 2024-11-18 18:38:00 发布