Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the rule defining the term for a constant expression have to be so cluttered?

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?

like image 839
Eugene Zavidovsky Avatar asked Dec 04 '15 15:12

Eugene Zavidovsky


1 Answers

which points to an object with storage duration other than static

  1. You ruled out null-pointers
  2. You ruled out function-pointers

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

  1. You omitted notion of references as members in objects. What to do with them? You can take const reference of runtime object or static object. Does this make whole object to be 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.

  • again we see mention of functions ( like with pointers ) - your rule (based on 5.20/2.9 ) lacks it.
  • there is no mention of reference to object with static storage duration in your rule
like image 128
Roman Zaitsev Avatar answered Oct 20 '22 00:10

Roman Zaitsev