2016-04-12 4 views
10

Die Spezifikation std::addressof wurde für C++ 17 geändert: Es darf nun ein konstanter Ausdruck sein. Allerdings, sagt cppreference dass:std :: addressof als konstanter Ausdruck in C++ 17

Der Ausdruck std::addressof(E) eine Konstante subexpression ist, wenn E ein L-Wert konstant subexpression ist.

  • Was ist ein konstanter Teilausdruck?
  • In welchem ​​Beispiel wird std::addressof(E) ein konstanter Ausdruck sein?
  • Was ist ein Beispiel, wo std::addressof(E) wird nicht ein konstanter Ausdruck sein?

Antwort

11

Dies wird erklärt here.

Führen Sie die folgende neue Definition der bestehenden Liste in 17,3 [Definitionen]: [Zeichen Anmerkung: Wenn LWG 2234 vor diesem Problem angenommen wird, soll der akzeptierte Wortlaut für die neue Definition stattdessen verwendet werden - Ende Ausarbeitung Anmerkung]

**constant subexpression** [defns.const.subexpr] 

an expression whose evaluation as a subexpression of a *conditional-expression* *CE* (5.16 [expr.cond]) would not prevent *CE* from being a core constant expression (5.20 [expr.const]). 

So "konstante subexpression" bedeutet in etwa "Sie es in einem konstanten Ausdruck verwenden können".

In welchem ​​Beispiel wird std :: addressof (E) ein konstanter Ausdruck sein?

ich es sollte glauben, einen konstanten Ausdruck geben, wenn &E hat (die & unter der Annahme, ruft die eingebauten Adress-of-Operator).

constexpr int x = 42; // static storage duration 
constexpr int* p1 = &x; // x is an lvalue constant subexpression 
constexpr int* p2 = std::addressof(x); // x is an lvalue constant subexpression 

Was ist ein Beispiel, in dem std :: AddressOf (E) wird nicht ein konstanter Ausdruck sein?

std::map<int, int> m; 
void f() { 
    int& r = m[42]; 
    constexpr int* z1 = &r; // error: r is not a constant subexpression 
    constexpr int* z2 = std::addressof(r); // likewise 

    constexpr int x = 43; // automatic storage duration 
    constexpr const int y1 = *&x;    // ok; x is a constant subexpression 
    constexpr const int y2 = *std::addressof(x); // likewise 
    constexpr const int* p1 = &x;    // error: p1 points to an automatic object 
    constexpr const int* p2 = std::addressof(x); // likewise 

} 
+0

so 'X' ist ein konstanter Ausdruck, aber keine Konstante subexpression, im zweiten Beispiel? Klar wie Schlamm –

+0

@ M.M Eigentlich ist 'x' auch ein konstanter Ausdruck im zweiten Beispiel ... Ich hätte mir wahrscheinlich ein besseres Beispiel aussuchen sollen. – Brian

+0

Ich sagte nicht eine Konstante * sub * Ausdruck –

Verwandte Themen