gpt4 book ai didi

c++ - 应用间接时,标准是否要求指针变量的左值到右值转换?

转载 作者:IT老高 更新时间:2023-10-28 21:57:37 26 4
gpt4 key购买 nike

TL;DR

给定以下代码:

int* ptr;
*ptr = 0;

*ptr 是否需要在应用间接之前对 ptr 进行 左值到右值 转换?

该标准在很多地方都涵盖了 lvalue-to-rvalue 的主题,但似乎没有指定足够的信息来确定 * 运算符 是否需要这样的转换。

详情

左值到右值的转换在 N3485 中有介绍。在 4.1 Lvalue-to-rvalue conversion 段落 1 中并说(强调我的前进):

A glvalue (3.10) of a non-function, non-array type T can be converted to a prvalue.53 If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the glvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior.[...]

*ptr = 0; 需要这种转换吗?

如果我们转到第 41 部分,它会说:

[...]A standard conversion sequence will be applied to an expression if necessary to convert it to a required destination type.

那么什么时候需要?如果我们查看 5 Expressions 部分,9 部分提到了 lvalue-to-rvalue 转换,它说:

Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue (4.1), array-to-pointer (4.2), or function-to-pointer (4.3) standard conversions are applied to convert the expression to a prvalue. [...]

和第11段说:

In some contexts, an expression only appears for its side effects. Such an expression is called a discarded-value expression.[...] The lvalue-to-rvalue conversion (4.1) is applied if and only if the expression is an lvalue of volatile-qualified type and it is one of the following [...]

这两个段落似乎都不适用于此代码示例和 5.3.1 一元运算符 段落 1 它说:

The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.” [ Note: indirection through a pointer to an incomplete type (other than cv void) is valid. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a prvalue, see 4.1. —end note ]

它似乎不需要指针的 ,而且我没有看到任何指针转换的要求,我错过了什么吗?

我们为什么要关心?

我在其他问题中看到了一个答案和评论,声称使用未初始化的指针是未定义的行为,因为需要 lvalue-to-rvalue 转换 ptr 在应用间接之前。例如:Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior?提出了这个论点,我无法将该论点与该标准的任何最新草案版本中的内容相一致。由于我已经多次看到这一点,我想得到澄清。

未定义行为的实际证明并不重要,因为正如我在上面的链接问题中指出的那样,我们还有其他方法可以解决未定义行为。

最佳答案

我认为你是从一个相当倾斜的角度来处理这个问题,可以这么说。根据§5.3.1/1:

The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is “pointer to T,” the type of the result is “T.”

虽然这里没有讨论左值到右值的转换,但它要求表达式是指向对象或函数的指针。一个未初始化的指针不会(除非,可能是偶然的)是任何这样的东西,所以取消引用的尝试会产生未定义的行为。

关于c++ - 应用间接时,标准是否要求指针变量的左值到右值转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21053273/

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