小编llx*_*xee的帖子

使用左值引用时为 SFINAE,但使用右值引用时成功

我搜索了但确实找不到答案,为什么仅当参数由左值引用传递时才会发生 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)

c++ templates sfinae c++11 c++17

0
推荐指数
1
解决办法
87
查看次数

标签 统计

c++ ×1

c++11 ×1

c++17 ×1

sfinae ×1

templates ×1