https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116476

            Bug ID: 116476
           Summary: error: binding reference of type 'int&&' to 'const
                    int' discards qualifiers for std::vector<field<int>>
                    f{2}; (OK under Clang and GCC<=13)
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nabijaczleweli at nabijaczleweli dot xyz
  Target Milestone: ---

minified reproducer, reduced from the original
(https://git.sr.ht/~nabijaczleweli/voreutils/tree/5781698e96ad3a085a2f3d712dc61b020ae287ad/item/cmd/df.cpp#L80):

#include <vector>
template <class T>
struct field {
    field(T &&) {}
};
std::vector<field<int>> fields_normal{2};       // Error
std::vector<field<int>> fields_workaround{{2}}; // OK

All Clangs I cared to try and GCCs <=13 accept this and do the expected thing.
GCC 14 (and trunk according to godbolt) explode with

<source>:6:40: error: binding reference of type 'int&&' to 'const int' discards
qualifiers
    6 | std::vector<field<int>> fields_normal{2};       // Error
      |                                        ^
<source>:4:11: note:   initializing argument 1 of 'field<T>::field(T&&) [with T
= int]'
    4 |     field(T &&) {}
      |           ^~~~
<source>: In function 'void __static_initialization_and_destruction_0()':
<source>:6:40: error: cannot bind rvalue reference of type 'int&&' to lvalue of
type 'const int'
    6 | std::vector<field<int>> fields_normal{2};       // Error
      |                                        ^
<source>:4:11: note:   initializing argument 1 of 'field<T>::field(T&&) [with T
= int]'
    4 |     field(T &&) {}
      |           ^~~~
Compiler returned: 1

which feels nonsensical to me.

Reply via email to