Bug 115085 - Variable unqualified-id is falsely treated as rvalue when appearing in braced-init-list
Summary: Variable unqualified-id is falsely treated as rvalue when appearing in braced...
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 15.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2024-05-14 09:38 UTC by Jan Schultke
Modified: 2024-05-15 13:28 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Schultke 2024-05-14 09:38:03 UTC
> int a{}, b = decltype((a)){a};

GCC falsely rejects this (https://godbolt.org/z/a6c4h8Mhv).
If you're having trouble reading this, it also rejects:

> using T = int&;
> int a{}, b = T{a};

Splitting this into multiple lines is also not relevant. The error is:

> <source>:3:17: error: cannot bind non-const lvalue reference of type 'T' {aka 'int&'} to an rvalue of type 'int'
>    3 | int a{}, b = T{a};
>      |                 ^

There is no apparent reason why should be an rvalue in this context. It is not move-eligible within the initializer of a variable.

My guess is something something aggregate initialization doing copy-initialization for each initializer and then getting a prvalue out of that. Dunno, it's quite weird.

See https://stackoverflow.com/a/78477064/5740428 for a more in-depth explanation of the relevant wording.
Comment 1 Jan Schultke 2024-05-14 09:41:19 UTC
Another user suggested that this is caused by falsely performing temporary materialization.

This would make a an xvalue, which would also make the reference binding fail.
Comment 2 Andrew Pinski 2024-05-14 09:50:39 UTC
I think there is a dup of this bug already filed ...
Comment 3 Andrew Pinski 2024-05-14 09:58:16 UTC
See https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1521 also.
Comment 4 Jan Schultke 2024-05-14 10:24:51 UTC
https://github.com/cplusplus/CWG/issues/536