3

N4527 5.20 [expr.const]p3

An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression.

5.20 [expr.const]p5

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 where, for that object and its subobjects:

(5.1) — each non-static data member of reference type refers to an entity that is a permitted result of a constant expression, and

(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.

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.

void foo(){
    const int a = 1;//a has automatic storage duration
    // all ok in gcc 5.1.0 and clang 3.8.0
    int b[a]{};
    static_assert(a,"");
    switch(1){
      case a:
        ;
    }
}

Question1: Is a an integral constant expression?

Question2: Is a a constant expression?

Question3: Is a glvalue integral constant expression a constant expression?

Question4:

If the answer of question 3 is yes, does this conflict with 5.20 p3 if the object has automatic storage duration?

stackcpp
  • 1,275
  • 7
  • 15
  • 2
    Um one question per question please. One. Not five. – Lightness Races in Orbit Jul 20 '15 at 23:24
  • @Lightness Races in Orbit The main question is 4, but if 3 is no, there's no necessary to ask 4, and so on. 1, 2, and 3 are part of 4. – stackcpp Jul 20 '15 at 23:30
  • `a` is a constant expression because of [expr.const]/(2.7.1). – Kerrek SB Jul 20 '15 at 23:30
  • @KerrekSB: Only if the lvalue-to-rvalue conversion is applied. – aschepler Jul 20 '15 at 23:33
  • @KerrekSB `a` is a **core constant expression** because of [expr.const]/(2.7.1). – stackcpp Jul 20 '15 at 23:38
  • Then reword it into a single question please. – Lightness Races in Orbit Jul 20 '15 at 23:38
  • @LightnessRacesinOrbit Does p5 conflict with p3 if the object has automatic storage duration? – stackcpp Jul 20 '15 at 23:46
  • @aschepler: Yes - it's hard to express that in syntax :-S – Kerrek SB Jul 20 '15 at 23:48
  • In your question! Not in a comment! – Lightness Races in Orbit Jul 20 '15 at 23:53
  • @LightnessRacesinOrbit 1, 2 and 3 are used to make sure people understand what i am asking. If you do, just skip them. – stackcpp Jul 21 '15 at 00:08
  • By your own quote, there's no such thing as a "glvalue integral constant expression", since an integral constant expression is, by definition, "an expression...implicitly converted to a prvalue". – T.C. Jul 21 '15 at 01:55
  • @T.C. _integral constant expression is an expression_, and 3.10p1 _Every expression belongs to exactly one of the fundamental classifications in this taxonomy: lvalue, xvalue, or prvalue._ So `const int a = 1;` expression `a` is an lvalue, if `a` is an integral constant expression, isn't this "glvalue integral constant expression"? – stackcpp Jul 21 '15 at 02:14
  • "An *integral constant expression* is an expression of integral or unscoped enumeration type, ***implicitly converted to a prvalue***". The glvalue expression `a` is not an integral constant expression. The glvalue expression `a` implicitly converted to a prvalue is an integral constant expression. – T.C. Jul 21 '15 at 02:33
  • According to these quotes, the expression `a` is: a prvalue core constant expression, and a glvalue, but not a glvalue core constant expression. This does seem like weird terminology. – M.M Jul 21 '15 at 03:06
  • @stackcpp: Don't call them "questions" then! – Lightness Races in Orbit Jul 21 '15 at 10:56
  • @LightnessRacesinOrbit Just one question, see http://stackoverflow.com/questions/31537359/const-int-a-1-is-a-a-constant-expression-if-a-has-automatic-storage-dura – stackcpp Jul 21 '15 at 11:08

1 Answers1

0

Is a an integral constant expression?

In the following contexts:

int b[a]{};
static_assert(a,"");
switch(1){
  case a:
    ;
}

yes, a is an integral constant expression. Starting with your first quote:

An integral constant expression is an expression of integral or unscoped enumeration type, implicitly converted to a prvalue, where the converted expression is a core constant expression.

'a' is an integral type, in your cases it will be implicitly converted to a prvalue, so now is a a core constant expression? Yes, if we go back to paragraph 2 which defines what is not a core constant expression:

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

it has the following clause:

an lvalue-to-rvalue conversion (4.1) unless it is applied to

with the following exception:

a non-volatile glvalue of integral or enumeration type that refers to a complete non-volatile const object with a preceding initialization, initialized with a constant expression, or

which applies to a since it is non-volatile, const and is initialized with a constant expression.


Is a a constant expression?

In the same contexts as above, yes, since we can see from the quote above it is a core constant expression.


Is a glvalue integral constant expression a constant expression?

No, in order for it to be a integral constant expression it must be converted to a prvalue and threfore can not be a glvalue.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • @T.C. @ Matt McNabb First, `a` is an id-expression, it is an lvalue expression, it is a core constant expression. Is it a integral constant expression? – stackcpp Jul 21 '15 at 03:18
  • @T.C. said "The glvalue expression `a` is not an integral constant expression." and @Shafik Yaghmour said "Yes". – stackcpp Jul 21 '15 at 03:36
  • Core constant expression before converted is integral constant expression, or after converted is integral constant expression? – stackcpp Jul 21 '15 at 03:43
  • @stackcpp so these are all in context of certain expressions, so a constant expression is a `conditional-expression` and so you are looking for places in the grammar where `conditional-expression` or `constant-expression` show up. – Shafik Yaghmour Jul 21 '15 at 03:53
  • My original thought is `const int a = 1;//a has automatic storage duration`, and according to 5.20p5, in constant expression, an object to which glvalue core constant expression refers must have static storage duration. So `a` is not constant expression, but it can be used as array boundary, so it is a integral constant expression. This is weird. A integral constant expression is not a constant expression. There must be wrong somewhere, but I can't find it. – stackcpp Jul 21 '15 at 04:07
  • see my new question http://stackoverflow.com/questions/31537359/const-int-a-1-is-a-a-constant-expression-if-a-has-automatic-storage-du – stackcpp Jul 21 '15 at 12:29
  • I think `a` is a lvalue core constant expression. Because `a` has automatic storage duration, so `a` is not a constant expression. But after lvalue-to-rvalue conversion, there will be a constant expression. – stackcpp Jul 21 '15 at 12:55
  • Or in context of `int b[a]{};`, 'a' is a constant expression. But in in context of `int &b = a;`, 'a` is not a constant expression. – stackcpp Jul 21 '15 at 12:58
  • @stackcpp that is what I said :-) let me look at rewording it, not sure how long that will take. – Shafik Yaghmour Jul 21 '15 at 13:23
  • @stackcpp the new question is very similar to this one, you should at least indicate that it is a clarification of this question although I am going to guess some would consider them duplicates. – Shafik Yaghmour Jul 21 '15 at 13:26