为什么一元 * 运算符没有约束 "the operand shall not be a pointer to void"?
Why the unary * operator does not have a constraint "the operand shall not be a pointer to void"?
C2x,6.5.3.2 地址和间接运算符,约束,2:
The operand of the unary * operator shall have pointer type.
为什么没有约束“操作数不应是指向 void
的指针”?
虽然可以推导出:
C2x,6.5.3.2 地址和间接运算符,语义,4:
The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object.
C2x,6.3.2.1 左值、数组和函数指示符,1:
An lvalue is an expression (with an object type other than void) that potentially designates an object; ...
一个 可能 (虽然有点做作,我承认)添加 'suggested' 约束会破坏代码的情况是 &
和 *
运算符被连接起来。在这种情况下,a = &*p
等表达式,其中 p
是 void*
类型, 是 允许的。
来自 this Draft Standard,立即 在您第一次引用的部分之后(粗体强调我的):
Semantics
3 The unary &
operator yields the address of its
operand. If the operand has type ‘‘type’’, the result has type
‘‘pointer to type’’. 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, except that the
constraints on the operators still apply and the result is not an
lvalue. …
目前,我无法为 &*
组合(在 void*
或任何其他指针类型上)想到一个 use-case – 但它 可能出现在“auto-generated”and/or使用条件宏扩展的代码中。
C2x,6.5.3.2 地址和间接运算符,约束,2:
The operand of the unary * operator shall have pointer type.
为什么没有约束“操作数不应是指向 void
的指针”?
虽然可以推导出:
C2x,6.5.3.2 地址和间接运算符,语义,4:
The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object.
C2x,6.3.2.1 左值、数组和函数指示符,1:
An lvalue is an expression (with an object type other than void) that potentially designates an object; ...
一个 可能 (虽然有点做作,我承认)添加 'suggested' 约束会破坏代码的情况是 &
和 *
运算符被连接起来。在这种情况下,a = &*p
等表达式,其中 p
是 void*
类型, 是 允许的。
来自 this Draft Standard,立即 在您第一次引用的部分之后(粗体强调我的):
Semantics
3 The unary&
operator yields the address of its operand. If the operand has type ‘‘type’’, the result has type ‘‘pointer to type’’. 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, except that the constraints on the operators still apply and the result is not an lvalue. …
目前,我无法为 &*
组合(在 void*
或任何其他指针类型上)想到一个 use-case – 但它 可能出现在“auto-generated”and/or使用条件宏扩展的代码中。