gpt4 book ai didi

c++ - 12.1 p14 中的 "unspecified value"是什么意思?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:46:59 25 4
gpt4 key购买 nike

12.1 p14 中术语未指定值的准确含义是什么:

During the construction of a const object, if the value of the object or any of its subobjects is accessed through a glvalue that is not obtained, directly or indirectly, from the constructor’s this pointer, the value of the object or subobject thus obtained is unspecified. [ Example:

struct C;
void no_opt(C*);

struct C {
int c;
C() : c(0) { no_opt(this); }
};

const C cobj;

void no_opt(C* cptr) {
int i = cobj.c * 100; // value of cobj.c is unspecified
cptr->c = 1;
cout << cobj.c * 100 // value of cobj.c is unspecified
<< ’\n’;
}

—end example ]

我可以在 1.3.25 中找到未指定行为的定义:

unspecified behavior
behavior, for a well-formed program construct and correct data, that depends on the implementation

  • 我在 VS2010 中运行这个片段,我得到了上面两个表达式 cobj.c 的正确结果:第一个 01 最后一个。这里没有说明什么?

  • 未指定行为的定义是否适用于未指定的值,即,这是否意味着在另一个编译器中我可以获得不同的结果?

最佳答案

我想说,关键是未指定的行为实现定义的行为有何不同:

1.3.10
implementation-defined behavior
behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation documents.

1.3.25
unspecified behavior
behavior, for a well-formed program construct and correct data, that depends on the implementation

未指定值 意味着不仅标准不需要特定值,而且实现甚至不必记录(预期)值(在这种情况下,值可能会改变不仅仅是从实现到实现,而是从版本到版本甚至从执行到执行)。这不同于实现定义的值。使用实现定义的值,您知道您将获得什么,但它因实现而异。对于未指定的值,您不知道会得到什么,因为它不一定记录在实现中,甚至从执行到执行也是如此。

关于c++ - 12.1 p14 中的 "unspecified value"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20829244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com