C++11:完美转发中的恒常性
c++11: constness in perfect forwarding
我创建了函数来检测参数的恒定性和l(r)值。
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?
std::is_const<T>
只检测顶级const
。喜欢foo const
,或者foo* const
。它不在乎"内部"const
,比如foo const*
或foo const&
。
如果你想要的是查看是否键入对 const 的引用,则需要先取出引用,这样const
就变成了顶级:
std::is_const<typename std::remove_reference<T>::type>::value
无论如何,显示的函数不允许类型推断,这意味着您必须显式传递T
,例如detect<foo const&>(x)
。也许你想要类似以下内容的东西?
template<class T> std::string
detect(T&&) { // have T be deduced
return std::string(std::is_const<typename std::remove_reference<T>::type>::value ? "const " : "")
+ (std::is_lvalue_reference<T>::value? "lvalue" : "rvalue");
}
可以像detect(x)
一样称为.
相关文章:
- const_cast静态数组以添加恒常性
- 自动模板参数、数据成员和恒常性
- 有没有办法检测自身内部的类恒常性?
- 为什么const_cast删除指针的恒常性,而不删除指向常量的指针的恒常性?
- 对指针和恒常性兼容性的引用
- 自定义容器中的括号运算符中的恒常性
- C++ 重载分辨率和恒常性
- C++17类模板演绎常性
- C++ 函数匹配和恒常性
- 如何忽略函数模板中的恒常性?
- 常量自动和对象恒常性
- 比较运算符 == 中的共享指针恒常性
- 添加额外的恒常性会导致编译器错误
- 删除模板化函数中的恒常性
- 无法正确理解我的恒常性
- 模板转换运算符优先级和恒常性
- const_iterator和恒常性const_iterator::value_type
- 使用const_cast来添加恒常性 - 坏主意?
- C++ 逻辑恒常性和 const方法的按值返回
- C++11:完美转发中的恒常性