c++ - Taking the address of a literal, not a `constexpr` -


strictly according rules of c++14, @ least ones given cppreference.com, isn't line (1) constant expression?

constexpr const int* addr(const int& ir) { return &ir; } constexpr const int* tp = addr(5); // (1) 

it's true not address constant expression, because &ir isn't address of static object (&ir address of temporary in context, cannot known in compilation-time).

but core constant expression because doesn't violate of back-listed rules of core constant expression, has no back-listed rules getting address of object.

no, addr(5) not constant expression, , therefore posted code ill-formed. yes, correct addr(5) core constant expression.

i can see why might think cppreference page alone core constant expressions, integral constant expressions, converted constant expressions, literal constant expressions, reference constant expressions, , address constant expressions types of constant expression. true definition given in c++14 [expr.const]/4:

a constant expression either glvalue core constant expression value refers object static storage duration or function, or prvalue core constant expression value object where, object , subobjects:

  • each non-static data member of reference type refers object static storage duration or function, and

  • if object or subobject of pointer type, contains address of object static storage duration address past end of such object, address of function, or null pointer value.

being "core constant expression" not have many direct implications; it's merely term used define "integral constant expression", "converted constant expression of type t", , "constant expression". , "constant expression" describes subset of expressions described "core constant expression", not superset.

for example, , complete, paragraph makes example ill-formed ([dcl.constexpr]/9) requires constant expression, not core constant expression, initializer.

a constexpr specifier used in object declaration declares object const. such object shall have literal type , shall initialized. if initialized constructor call, call shall constant expression. otherwise, or if constexpr specifier used in reference declaration, every full-expression appears in initializer shall constant expression. [note: each implicit conversion used in converting initializer expressions , each constructor call used initialization part of such full-expression. - end note]


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -