简体   繁体   English

const值运行时评估

[英]const values run-time evaluation

The output of the following code: 以下代码的输出:

const int i= 1;
(int&)i= 2;          // or:  const_cast< int&>(i)= 2;
cout << i << endl;

is 1 (at least under VS2012) 1 (至少在VS2012下)

My question: 我的问题:

  • Is this behavior defined? 是否定义了此行为?
  • Would the compiler always use the defined value for constants? 编译器是否总是使用定义的常量值?
  • Is it possible to construct an example where the compiler would use the value of the latest assignment? 是否有可能构建一个编译器将使用最新赋值的示例?

It is totally undefined. 它完全没有定义。 You just cannot change the value of constants. 你只是不能改变常量的值。

It so happens that the compiler transforms your code into something like 碰巧的是,编译器会将您的代码转换为类似的代码

cout << 1 << endl;

but the program could just as well crash, or do something else. 但程序也可能崩溃,或做其他事情。

If you set the warnings level high enough, the compiler will surely tell you that it is not going to work. 如果将警告级别设置得足够高,编译器肯定会告诉您它不起作用。

Is this behavior defined? 是否定义了此行为?

The behavior of this code is not defined by the C++ standard, because it attempts to modify a const object. 此代码的行为未由C ++标准定义,因为它尝试修改const对象。

Would the compiler always use the defined value for constants? 编译器是否总是使用定义的常量值?

What value the compiler uses in cases like this depends on the implementation. 在这种情况下,编译器使用什么值取决于实现。 The C++ standard does not impose a requirement. C ++标准没有强制要求。

Is it possible to construct an example where the compiler would use the value of the latest assignment? 是否有可能构建一个编译器将使用最新赋值的示例?

There might be cases where the compiler does modify the value and use it, but they would not be reliable. 可能存在编译器修改值并使用它的情况,但它们不可靠。

The answer is that the behavior is undefined. 答案是行为未定义。

I managed to set up this conclusive example: 我设法建立了这个确凿的例子:

#include <iostream>

using namespace std;

int main(){

        const int i = 1;

        int *p=const_cast<int *>(&i);
        *p = 2;
        cout << i << endl;

        cout << *p << endl;

        cout << &i << endl;

        cout << p << endl;

        return 0;
}

which, under gcc 4.7.2 gives: 其中,在gcc 4.7.2下给出:

1
2
0x7fffa9b7ddf4
0x7fffa9b7ddf4

So, it is like you have the same memory address as it is holding two different values. 所以,就像你拥有相同的内存地址,因为它拥有两个不同的值。

The most probable explanation is that the compiler simply replaces constant values with their literal values. 最可能的解释是编译器只是用它们的文字值替换常量值。

As said by others, the behaviour is undefined. 正如其他人所说,行为是不确定的。

For the sake of completeness, here is the quote from the Standard: 为了完整起见,以下是标准的引用:

(§7.1.6.1/4) Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior. (§7.1.6.1/ 4)除了可以修改任何声明为可变的类成员(7.1.1)之外,任何在其生命周期内修改const对象的尝试(3.8)都会导致未定义的行为。 [ Example: [例如:

[...] [...]

 const int* ciq = new const int (3); // initialized as required int* iq = const_cast<int*>(ciq); // cast required *iq = 4; // undefined: modifies a const object 

] ]

Note that the word object is this paragraph refers to all kinds of objects, including simple integers, as shown in the example – not only class objects. 注意,单词object是这个段落引用的各种对象,包括简单的整数,如示例所示 - 不仅仅是类对象。

Although the example refers to a pointer to an object with dynamic storage, the text of the paragraph makes it clear that this applies to references to objects with automatic storage as well. 尽管该示例引用了指向具有动态存储的对象的指针,但该段落的文本清楚地表明这也适用于对具有自动存储的对象的引用。

You are doing a const_cast using the C-like cast operator . 您正在使用类似C的const_cast cast operator执行const_cast

Using const_cast is not guaranteeing any behaviour. 使用const_cast不保证任何行为。

if ever you do it, it might work or it might not work. 如果你这样做,它可能会起作用,也可能不起作用。

(It's not good practice to use C-like operators in C++ you know) (你知道在C ++中使用类似C的运算符是不好的做法)

Yes you can, but only if you initiate a const as a read-only but not compile-time const, as follows: 是的,你可以,但只有当你将const作为只读但不是编译时const时,如下所示:

int y=1;
const int i= y;
(int&)i= 2;
cout << i << endl; // prints 2

C++ const keyword can be missleading, it's either a const or a read-only. C ++ const关键字可以是missleading,它可以是const或只读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM