Dr. Gut
Dr. Gut

Reputation: 2836

What is an example where the result of lvalue-to-rvalue conversion removes cv-qualifiers?

Under Lvalue-to-rvalue conversion the C++23 standard says ([conv.lval]#1, emphasis mine):

A glvalue of a non-function, non-array type T can be converted to a prvalue.44 If T is an incomplete type, a program that necessitates this conversion is ill-formed. If T is a non-class type, the type of the prvalue is the cv-unqualified version of T. Otherwise, the type of the prvalue is T.45

Could you give an example, where this happens?

i.e:

Upvotes: 0

Views: 125

Answers (1)

user17732522
user17732522

Reputation: 76688

For example:

const int i = 0;
/*const*/ int j = i;

i is a non-class type const-qualified glvalue. In order to initialize j, a standard conversion sequence from this glvalue to a prvalue of type int is required. The lvalue-to-rvalue conversion is chosen and your particular case applies.

The same applies e.g. when using the built-in arithmetic operators on i, etc. Whether there is a qualifier on j doesn't matter. They are ignored and the target of the conversion sequence is the unqualified type.

Also note that non-class type prvalues are never cv-qualified, so it wouldn't make sense for the conversion to not strip the qualifier.

Upvotes: 2

Related Questions