'Why the unary * operator does not have a constraint "the operand shall not be a pointer to void"?
C2x, 6.5.3.2 Address and indirection operators, Constraints, 2:
The operand of the unary * operator shall have pointer type.
Why there is no constraint "the operand shall not be a pointer to void"?
Though it can be deduced from:
C2x, 6.5.3.2 Address and indirection operators, Semantics, 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 Lvalues, arrays, and function designators, 1:
An lvalue is an expression (with an object type other than void) that potentially designates an object; ...
Solution 1:[1]
One possible (though somewhat contrived, I'll admit) case where adding your 'suggested' constraint would break code is where the & and * operators are concatenated. In such cases, an expression such as a = &*p, where p is a void* type, is allowed.
From this Draft Standard, immediately following the section in your first citation (bold emphasis mine):
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. …
I can't, currently, think of a use-case for that &* combination (on a void* or any other pointer type) – but it may occur in code that is "auto-generated" and/or uses conditional macro expansion(s).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
