Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CWG2941 [class.temporary] Lifetime extension for RefType{} #625

Open
hubert-reinterpretcast opened this issue Oct 18, 2024 · 3 comments
Open

Comments

@hubert-reinterpretcast
Copy link

hubert-reinterpretcast commented Oct 18, 2024

Full name of submitter (unless configured in github; will be published with the issue): Hubert Tong

Reference (section label): [class.temporary]

Link to reflector thread (if any): N/A

Issue description:
CWG 2894 will clarify that RefType{} produces a glvalue (instead of a prvalue). Once that happens, [class.temporary] will require an update to allow lifetime-extension of any temporary bound to the reference initialized by the function-style cast expression. In particular, the function-style cast is not mapped to static_cast, etc. in this case and the temporary materialization occurs within the initialization for the reference formed by the cast itself.

Implementations appear to agree already that such lifetime-extension occurs (https://godbolt.org/z/EWczhzaf6):

int glob;
struct A {
  constexpr ~A() { p = &glob; }
  int *p;
};
constexpr int f() {
  typedef const A &AR;
  const A &ref = AR{0};
  delete ref.p;
  return 0;
}
extern constexpr int x = f();  // okay

Suggested resolution:
Add a bullet to [class.temporary]/6:

  • a

    [ ... ]

  • a function-style cast to a reference type where the reference is

    • bound directly to a glvalue operand that is one of these expressions
    • or bound to the result of a temporary materialization conversion
@jensmaurer jensmaurer changed the title [class.temporary] Lifetime extension for RefType{} CWG2941 [class.temporary] Lifetime extension for RefType{} Oct 18, 2024
@jensmaurer
Copy link
Member

CWG2941

@hubert-reinterpretcast
Copy link
Author

@jensmaurer, thanks for creating CWG 2941.

Some comments:

  • In the write-up, I think "a function-style casts" should be adjusted to "a function-style casts".
  • In the suggested wording, I think relying on the cross-reference to disambiguate between the functional notation and the cast notation ([expr.cast]) for "explicit type conversion" is not ideal. "Function-style cast" is used in the majority of cases.
  • In the suggested wording (my fault), I think we need to say:
    • a function-style cast (7.6.1.4 [expr.type.conv]) whose initializer is a braced-init-list that is to a reference type where the reference is
      • bound directly to a glvalue operand that is the glvalue result of one of these expressions that is the sole element of the braced-init-list
      • or bound to the result of a temporary materialization conversion,

@jensmaurer
Copy link
Member

@hubert-reinterpretcast , thanks for your feedback.

  • typo in write-up fixed (going singular all the way)
  • I disagree with the "majority of cases". "function-style cast" appears a number of times for the discussion of parsing ambiguities, but a lot of other places talk about "explicit type conversion", sometimes with just the cross-reference, and sometimes with "(functional notation)" plus the cross-reference. I'm heading for the latter.
  • I've updated the suggested resolution, switching the word order for the condition to something that I can read better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants