2

[dcl.constexpr] p10 sentence 3 says:

In any constexpr variable declaration, the full-expression of the initialization shall be a constant expression

However, in this declaration:

constexpr int a = 10;
constexpr int b = a;

a is not a constant expression, as it is glvalue core constant expression, but not a permitted result of a constant expression because it does not have static storage duration, and is not a temporary object.

However, with the application of an lvalue-to-rvalue conversion, it will become a constant expression. So does that mean that the initializer does not need to be a constant expression, and only the final result after conversions has to be?

2
  • It seems a defect in the standard that the value category of a full-expression is not defined. Commented Aug 8, 2019 at 2:41
  • "It seems a defect in the standard that the value category of a full-expression is not defined." Seems to me incorrect. const.expr#11 already gives it a value category which is either a glvalue or prvalue. I don't see any defect here. Commented Sep 29, 2022 at 6:12

2 Answers 2

3

In the link you quoted, see point 10:

A constant expression is either a glvalue core constant expression that refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value satisfies the following constraints:

Your question focused on "permitted result of a constant expression" under the glvalue branch; however in your example it is the other branch "prvalue core constant expression" that applies. This can apply because of [conv.lval]/1,

A glvalue of a non-function, non-array type T can be converted to a prvalue

I agree it is a bit confusing that by "prvalue core constant expression" here they include the case of the result of lvalue-to-rvalue conversion on glvalues that meet the criteria; whereas in some other places in the standard "prvalue" excludes that case.

Sign up to request clarification or add additional context in comments.

6 Comments

perhaps I am misinterpreting what a full-expression is, particularly when the wording says that conversions are "part of the full-expression". A full expression is an expression, so if a conversion is part of that expression, does that mean that the inner expression is effectively "nested" inside the conversion, so when the full-expression is evaluated, it's the nested expression with that conversion applied being the result?
@KrystianS I think the part about the "full-expression of the initialization" is saying that the initializer (after any conversions to it are applied) should be a constant expression
Ok, so would a good definition for full-expression be "a full-expression is the outermost expression or conversion that is evaluated when a statement is executed"?
Actually, no, it can't be, because a full-expression is not necessarily a single expression.
The standard defines full-expression, you can consult that
|
1

The word "full-expression" is a defined term. Notably ([intro.execution]/5)

Conversions applied to the result of an expression in order to satisfy the requirements of the language construct in which the expression appears are also considered to be part of the full-expression.

So yes, since the requirement says "the full-expression of the initialization shall be a constant expression", it means only the full-expression (which includes the conversion), not anything else, is required to be a constant expression.

7 Comments

That's wasn't the question - rather what is the "initialization"? Obviously it includes the conversions, but does it include the initializer? The full expression of an initializer is itself, and any other conversions that are applied, but it is not clear if "initialization" includes the initializer expression itself.
@KrystianS The full-expression is just the one in the initializer.
@L.F. Ok, but in the example I showed in the question a isn't a constant expression
@KrystianS a is definitely a constant expression. If a constexpr variable is not a constant expression, what can possibly be?
@L.F. I think the wording makes sense (however, initialization may be a typo for initializer) and what is the issue is how I am interpreting what a full expression is. Since conversions are considered part of a full expression, would that mean that the initializer is effectively "nested" inside of the conversion, so when the full-expression is evaluated, its result is the result of the enclosing conversion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.