为什么C++常量引用可以折叠为非常量引用

Why can C++ const references be collasped into non-const references

本文关键字:引用 常量 非常 折叠 C++ 为什么      更新时间:2023-10-16

请考虑以下C++程序:

#include <iostream>
template<typename T>
class A
{
public:
explicit A(T& x) : x_(x){}
const T& get() { return x_; }
private:
T x_;
};
int main()
{
int x = 42;
A<int&>(x).get() = 43; // compiles fine, even though get() looks like it returns a const ref
std::cout << x << 'n';
}

程序编译 OK 并输出 43。这表明 get(( 返回的看似常量引用实际上是一个非常量引用,因为它允许修改它引用的值。

是导致这种行为的参考规则崩溃吗?

如何强制从 get(( 返回的引用的行为类似于 const 引用,也就是说,它不允许修改它引用的值?

是导致这种行为的参考规则崩溃吗?

是的。你有:

T = int&
const T& = const (int&) &

引用不能const(无论如何都不能重新绑定它们,所以它被忽略(,对引用的引用只是一个引用。

所以你有

const T& = int&

若要解决此问题,需要将const应用于基础类型,可以通过删除引用来执行此操作:

const std::remove_reference_t<T>& get() { return x_; }
//    ^^^^^^^^^^^^^^^^^^^^^^^