I do not understand why a temporary object, which is referred to by a prvalue core constant expression, must not have any pointer or a reference, which points or refers to an object with storage duration other than static, in order for that prvalue core constant expression to be a constant expression; see N4296 [expr.const] 5.20\5.
I think it is possible for implementations to rewrite that rule like this:
[expr.const] 5.20\5 (modified)
A core constant expression e is a constant expression, unless it initializes an object such that it contains a pointer, which points to an object with storage duration other than static.
If it is not, can someone, please, explain why?
which points to an object with storage duration other than static
From standard 5.20\5:
(5.2) if the object or subobject is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object (5.7), the address of a function, or a null pointer value
core constant expression
or not?(5.1) each non-static data member of reference type refers to an entity that is a permitted result of a constant expression
I bet there is an additional nuance with glvalue core constant expression
and prvalue core constant expression
( as I understand this phrase excludes some of xvalue
), but I fail to grasp it. I hope that someone will explain this.
A constant expression is either a glvalue core constant expression whose value refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value is an object
EDIT
So what is a problem with that? There are still other rules, you know: 5.20\2.7, 2.9, 2.13, 2.19, 2.5; etc. ; )
Yes, 5.20/2.9 is about references ( other mentioned rules doesn't apply to reference types - as I understand them ). Let's read it.
(2) A conditional-expression e is a core constant expression unless the evaluation of e , following the rules of the abstract machine ( 1.9 ), would evaluate one of the following expressions:
(2.9) an id-expression that refers to a variable or data member of reference type unless the reference has a preceding initialization and either
— (2.9.1) it is initialized with a constant expression or
— (2.9.2) it is a non-static data member of an object whose lifetime began within the evaluation of e ;
Now let's compare your rule and standard's. Standard adds some details to 5.20/2.9.
5.20/5
(5.1) each non-static data member of reference type refers to an entity that is a permitted result of a constant expression
An entity is a permitted result of a constant expression if it is an object with static storage duration that is either not a temporary object or is a temporary object whose value satisfies the above constraints, or it is a function.
functions
( like with pointers ) - your rule (based on 5.20/2.9 ) lacks it.object with static storage duration
in your ruleIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With