在C ++中是否定义了& * NULL? [英] Is &*NULL well-defined in C++?
问题描述
以下是什么定义明确的C ++标准版本(如果有)?
In what version(s) of the C++ standards (if any) is the following well-defined?
void foo(void) {
char *nullPtr = NULL;
&*nullPtr;
}
请注意,我在这里专门询问有关&*nullPtr
的问题.我知道*nullPtr
只是未定义-但这是一个单独的问题,因此当前链接的重复"不是重复的.
Note that I am specifically asking about &*nullPtr
here. I am aware that simply *nullPtr
is undefined - but this is a separate question and hence the currently-linked "duplicate" is not a duplicate.
请注意,我没有将结果分配给任何内容-第二行是一个简单的语句.
Note that I am not assigning the result to anything - the second line is a simple statement.
这个应该是一个带有明显答案的问题,但是(似乎在此类问题上经常发生)我听到很多人说答案显然没有定义"为明显"定义".
This should be a question with an obvious answer, but (as seemingly happens way too often on such questions) I have heard just as many people say the answer is "obviously undefined" as "obviously defined".
相对来说,以下内容如何? foo
是否应该读取c?
On a rather related note, what about the following? Should foo
produce a read of c?
extern volatile char c;
void bar(void) {
volatile char *nonnullptr = &c;
&*nonnullptr;
}
(相同问题的C版本:在以下位置定义的& * NULL是否为NULL C?)
(C version of the same question: Is &*NULL well-defined in C?)
推荐答案
这是四个问题合而为一.
This is four questions in one.
&*nullPtr
在C语言中定义良好,表示一元&
运算符:
&*nullPtr
is well-defined in C since C99, which says of the unary &
operator:
如果操作数是一元
*
运算符的结果,则两者都不是 运算符或&
运算符都被求值,结果就像 被省略了,[...]
If the operand is the result of a unary
*
operator, neither that operator nor the&
operator is evaluated and the result is as if both were omitted, [...]
&*nullPtr
在C ++的所有修订版中都未正式定义(通过省略:一元&
被指定为产生指向指定对象"的指针,一元*
被指定为产生指向该对象的左值"表达式指向的对象;"空指针值指向没有对象),尽管核心问题的方向232 是为了使它定义明确.
&*nullPtr
is formally undefined in all revisions of C++ (by omission: unary &
is specified to produce a pointer to "the designated object", and unary *
is specified to produce "an lvalue referring to the object [...] to which the expression points"; a null pointer value points to no object), although the direction of core issue 232 is to make this well-defined.
&*nonnullptr
不会产生*nonnullptr
的易失性读取.一元&
需要一个左值操作数; *nonnullptr
不执行左值转换(对于C)或左值到右值转换(对于C ++).
&*nonnullptr
produces no volatile read of *nonnullptr
. Unary &
expects an lvalue operand; no lvalue conversion (for C) or lvalue-to-rvalue conversion (for C++) is performed for *nonnullptr
.
这篇关于在C ++中是否定义了& * NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!