const&指非易失性变量。变量改变。更改会使const&无效吗?
在C ++中, const&
的值可以更改吗?
In C++, can the value of a const &
change?
当然,它不能改变,可以吗?这就是 const
的意思。此外,请听Stroustrup:
Well, of course it cannot change, can it? That's what const
means. Moreover, listen to Stroustrup:
A
const
左值引用是指常量,
A
const
lvalue reference refers to a constant, which is immutable from the point of view of the user of the reference.
但是这怎么办?
#include <iostream>
int main() {
int a = 0;
const int& r = a;
const int old_r = r;
++a;
const int new_r = r;
std::cout
<< "old_r == " << old_r
<< " but new_r == " << new_r << std::endl;
return 0;
}
在我的计算机上,此输出为 old_r == 0,但new_r == 1
。
On my machine, this outputs, old_r == 0 but new_r == 1
.
这是我真正的问题。在上面的代码中,查看以下行
That gets to my real question. In the above code, look at the line
const int new_r = r;
只要
- 地址
& new_r
既不在此行也不在代码中的其他位置提取,并且 - 该代码没有
volatile
,
- the address
&new_r
is extracted neither on this line nor elsewhere in the code and - the code has nothing
volatile
,
任何事情都会阻止优化编译器合并 $ _b
和
$将old_r new_r
放入单个常量对象,将其视为如下所示?
does anything prevent an optimizing compiler from merging old_r
and new_r
into a single constant object, treating the line as though it read as follows?
const int& new_r = old_r;
我之所以问是因为,据我所知,如果编译器进行了优化,那可能会改变行为。该程序可能会输出 old_r == 0但new_r == 0
。
I ask because, as far as I know, if the compiler did so optimize, that might alter the behavior. The program might output, old_r == 0 but new_r == 0
.
相关问题
我发现的最相关的现有问题是:
The most nearly related existing question I find is this one:
- (C语言) 可以更改const吗? (尤其请参阅链接的问题的要点。 1。)
以下内容也是相关的,但与当前问题不同,它涉及强制转换:
The following are also related but, unlike the present question, involve casts:
- 更改const的值
- 在同一内存地址有两个不同的值
- 修改常量对象
- 更改const变量的值[重复]
- (C语言)通过const声明访问非const
- (C语言)我们可以修改const变量的值吗?
- (C语言)使用指针更改常量变量值
- (C语言)关于使用指针修改const变量的困惑
- (C语言)使用const_cast的怪异行为[重复]
- Changing the value of a const
- Two different values at the same memory address
- Modifying constant object
- Changing the value of const variable [duplicate]
- (C language) Accessing a non-const through const declaration
- (C language) Can we modify the value of a const variable?
- (C language) Const variable value is changed by using a pointer
- (C language) Confusion regarding modification of const variable using pointers
- (C language) Weird Behaviour with const_cast [duplicate]
另请参见 N4659 (C ++ 17标准草案),第10.1.7.1节, cv限定词。
See also N4659 (draft C++17 standard), sect. 10.1.7.1, "The cv-qualifiers."
Stroustrup在问题顶部的引文来自第
The quote of Stroustrup at the top of the question comes from sect. 7.7.2 of The C++ Programming Language, 4th ed. Of course, no author can write every sentence perfectly in a thousand-page book; yet perhaps Stroustrup is clear and I have merely read him wrong. Nevertheless, you might see why the sentence has confused me. This is why I have asked.
在C ++中, const& 更改?
是,但不能通过该引用(忽略 mutable
字段)。
Yes, but not through that reference (ignoring mutable
fields).
void foo(const int& c, int& mut) {
std::cout << c << " ";
++mut; // changes `c` if &c == &mut
std::cout << c << std::endl;
}
和
int a = 42;
foo(a, a); // 42 43
不会阻止优化编译器合并old_r和将new_r放入单个常量对象中,将这行内容看成如下所示?
does anything prevent an optimizing compiler from merging old_r and new_r into a single constant object, treating the line as though it read as follows?
as-if规则允许编译器优化是否可见的副作用是相同的,
此处不是。因此,幸运的是,您的代码中提议的变量合并无法完成。
The as-if rule allows compiler to optimize if visible side effect are the same,
which is not the case here. So your "proposed merge of variable" in your code cannot be done fortunately.