我创建了函数来检测参数的 constness 和 l(r)valueness。
template<class T> std::string
detect(typename std::remove_reference<T>::type&&) {
return std::string(std::is_const<T>::value ? "const " : "") + "rvalue";
}
template<class T> std::string
detect(typename std::remove_reference<T>::type&) {
return std::string(std::is_const<T>::value ? "const " : "") + "lvalue";
}
出于某种原因,即使在 const 类型(例如 const int&)上,is_const 也总是返回 false。我尝试添加另一个重载来捕获常量
template<class T> std::string
detect(const typename std::remove_reference<T>::type& ) { return "const lvalue"; }
然后编译器抱怨当应用于 const int& 时检测不明确。所以我认为编译器已经正确计算出 T=const int&,但为什么 is_const 不返回 true?