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?

4

1 に答える 1

0

a整数定数式ですか?

次のコンテキストで:

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

はい、a整数定数式です。最初の見積もりから始めます:

整数定数式は、暗黙的に prvalue に変換された整数またはスコープなし列挙型の式であり、変換された式はコア定数式です。

「a」は整数型です。あなたの場合、それは暗黙的に prvalue に変換されるので、今はaコア定数式ですか? はい、コア定数式ではないものを定義する段落 2 に戻ると、次のようになります。

条件式 e は、抽象マシン (1.9) の規則に従って e を評価した場合に次の式のいずれかが評価されない限り、コア定数式です。

次の句があります。

適用されない限り、左辺値から右辺値への変換 (4.1)

ただし、次の例外があります。

定数式で初期化された、前に初期化された完全な不揮発性 const オブジェクトを参照する整数型または列挙型の不揮発性 glvalue、または

aこれは、非揮発性であり、const であり、定数式で初期化されるため、に適用されます。


a定数式ですか?

上記と同じコンテキストでは、はい、上記の引用からわかるように、これはコア定数式です。


glvalue 整数定数式は定数式ですか?

いいえ、それを整数定数式にするためには、prvalue に変換する必要があるため、glvalue にすることはできません。

于 2015-07-21T02:43:43.070 に答える