const&指非易失性变量。变量改变。更改会使const&无效吗? [英] A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?

查看:135
本文介绍了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:

以下内容也是相关的,但与当前问题不同,它涉及强制转换:

The following are also related but, unlike the present question, involve casts:

  • 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在问题顶部的引文来自第 C ++编程语言,第四版。当然,没有作者能在一千页的书中完美地写出每一个句子。但也许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 ++中,<$ c的值是否可以$ 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.

这篇关于const&amp;指非易失性变量。变量改变。更改会使const&amp;无效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆