【发布时间】:2018-07-01 00:24:00
【问题描述】:
我假设decltype(auto) 在用于尝试和 SFINAE 关闭返回类型时是一个不兼容的构造。所以,当你本来会得到一个替换错误时,你会得到一个硬错误
但是为什么下面的程序可以工作? https://wandbox.org/permlink/xyvxYsakTD1tM3yl
#include <iostream>
#include <type_traits>
using std::cout;
using std::endl;
template <typename T>
class Identity {
public:
using type = T;
};
template <typename T>
decltype(auto) construct(T&&) {
return T{};
}
template <typename T, typename = std::void_t<>>
class Foo {
public:
static void foo() {
cout << "Nonspecialized foo called" << endl;
}
};
template <typename T>
class Foo<T,
std::void_t<typename decltype(construct(T{}))::type>> {
public:
static void foo() {
cout << "Specialized foo called" << endl;
}
};
int main() {
Foo<Identity<int>>::foo();
Foo<int>::foo();
}
当Foo 被int 实例化时,我们不应该得到一个硬错误吗?鉴于 int 没有名为 type 的成员别名?
【问题讨论】: