2

I always thought that a reference would be subjected to an lvalue-to-rvalue-conversion, as any other glvalue, when used in an expression. Nevertheless, it seems like, every time a reference is used in an expression, it is handled in bullet point (2.9) of [expr.const]/2 instead of bullet point (2.7) (in C++14, or C++1z).

Take for example the reference r below, used to initialize variable j. Is it subjected to an lvalue-to-rvalue-conversion?

const int i = 1; constexpr int& r = i constexpr int j = r; 

According to this answer the reference r is handled in bullet point (2.9) of [expr.const]/2 and not in bullet point 2.7, as I would expect. Why is this?

10
  • The bullets in [expr.const]/2 don't "handle" anything. Every bullet needs to be satisfied in order for the expression to be a core constant expression. Commented Jul 11, 2016 at 18:48
  • Then why isn't bullet (2.7) satisfied by the reference r? Could you explain? I mean why isn't one of the sub-bullets in (2.7) satisfied? Commented Jul 11, 2016 at 18:57
  • What part about every bullet is hard to understand? You need to satisfy all of them, not one of them. Commented Jul 11, 2016 at 18:58
  • Did you see my edit above? Commented Jul 11, 2016 at 18:59
  • Whether or not you satisfy 2.7, you still have to satisfy 2.9, and 2.10, and 2.11, and 2.12, and so forth... ... If it doesn't satisfy 2.9, it's not a core constant expression, regardless of whether 2.7 is met. Commented Jul 11, 2016 at 19:02

1 Answer 1

2

In some contexts the lvalue-to-rvalue conversion occurs because it is explicitly specified to occur in a given context (for example, for the ternary conditional operator, see here). But it is listed in clause 4 so it is an implicit standard conversion; like all other implicit standard conversions, it occurs when needed. For example, a glvalue of type int will be implicitly converted to a prvalue when used as the operand of an arithmetic expression since its stored value is required.

In the case of constexpr int j = r, yes, the glvalue expression r undergoes lvalue-to-rvalue conversion, since this initialization requires the stored value. Although it isn't explicitly specified that reading the stored value of an object invokes an lvalue-to-rvalue conversion, this fact must obviously be true in the context of the entire standard, as well as the C standard, where the term "rvalue" is not used, but instead the analogous concept of the lvalue conversion refers to the conversion of an lvalue into "the value stored in the designated object".

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.