cplusplus / cplusplus/draft

[class.temporary] p6.6 "glvalue operand" is misleading

Open
#5,352 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TeX
Stars
221
Forks
813
Avg merge
16h 4m
Merged PRs (30d)
36

Description

[class.temporary] p6.6 states that lifetime extension is applied to the temporary object referred to by "a [cast expression] converting, without a user-defined conversion, a glvalue operand that is one of these expressions to a glvalue that refers to the object designated by the operand, or to its complete object or a subobject thereof,". This appears to indicate that the following code has a dangling reference

class S{};
S&& s = static_cast<S&&>(S{});

because S{} is a prvalue, whereas in fact, according to [expr.static.cast] p4, the temporary materialization conversion is applied to the operand of a static cast, meaning that static_cast<S&&>(S{}) is a conversion of a glvalue to a glvalue.

The same logic applies to const_cast, because converting a prvalue to a glvalue also performs the temporary materialization conversion, see [expr.const.cast] p4. [expr.dynamic.cast] p2 and [expr.reinterpret.cast] p11 explicitly ban conversions of prvalues to glvalues.

Thus, using the term "glvalue operand" in p6.6 serves only to confuse the reader that is not already intimately familiar with [expr.static.cast] and [expr.const.cast], and means nothing at all to those who are because it can be deduced from context.

None of p6.1-6.5 explicitly specify that the expression shall be a glvalue, because all expressions described in p6 are already glvalues. p6.7 and p6.8 are correct because a conditional expression or comma expression returning a prvalue falls under p6.1, as the temporary is materialized from the result rather than within the expression.

I believe that p6.6 would be clearer and suffer no loss of precision if it read "[...] an operand that is one of these expressions to a glvalue that [...]".

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with [class.temporary] p6.6 and compare its "glvalue operand" wording with the referenced [expr.static.cast] p4 and [expr.const.cast] p4 rules. Check [expr.dynamic.cast] p2 and [expr.reinterpret.cast] p11 for consistency; done when p6.6 clearly states the intended operand requirement without losing precision.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.