c++ - 对 const 的右值引用有什么用吗?

标签 c++ c++11 constants rvalue-reference

我猜不是,但我想确认一下。 const Foo&& 有什么用,其中 Foo 是类类型?

最佳答案

它们有时很有用。 C++0x 草案本身在一些地方使用它们,例如:

template <class T> void ref(const T&&) = delete;
template <class T> void cref(const T&&) = delete;

上述两个重载确保其他 ref(T&)cref(const T&) 函数不绑定(bind)到右值(否则可能)。

更新

我刚刚查了官方标准N3290 ,不幸的是,它没有公开可用,它在 20.8 函数对象 [function.objects]/p2 中:

template <class T> void ref(const T&&) = delete;
template <class T> void cref(const T&&) = delete;

然后我查看了最新的 C++11 后草案,该草案是公开的,N3485 ,并且在 20.8 Function objects [function.objects]/p2 中它仍然说:

template <class T> void ref(const T&&) = delete;
template <class T> void cref(const T&&) = delete;

关于c++ - 对 const 的右值引用有什么用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4938875/

相关文章:

c++ - 如何停止 quickfix-initiator

c++ - 特征值:从 vector 中减去标量

c++ - std::tuple 的实现细节

c++ - 从 vector 中删除空元素

python - 将 .h 文件中的常量导入 python

vba - VBA 中 msoTextOrientationHorizo​​ntal 的常量值是多少?

c++ - OSX OpenGL 设置代码中的竞争条件

c++ - 使用包含 std::unique_ptr 的结构的 std::vector 声明类时出错

c++ - 为模板类重载 const 版本的 [] 运算符

C++11 强制转换const迭代器指向shared_ptr对象的容器