我搜索了但确实找不到答案,为什么仅当参数由左值引用传递时才会发生 SFINAE,但当参数由右值引用传递时构建会成功:
template <typename T>
class A {
public:
using member_type = T;
};
template <typename AType>
typename AType::member_type f(AType&& m) {
typename AType::member_type res{};
return res;
}
void demo() {
A<int> a;
// ERROR: candidate template ignored: substitution failure
// [with AType = A<int> &]: type 'A<int> &'
// cannot be used prior to '::' because it has no members
f(a);
// BUILD SUCCESS
f(std::move(a));
}
Run Code Online (Sandbox Code Playgroud)