Issue 157283
Summary rejects valid on defaulted default constructor for 'std::vector<forwarded_class>'
Labels c++, rejects-valid
Assignees
Reporter wheatman
    I think the following valid code is being rejected 
```cpp
#include <vector>

class forwarded_class;

struct foo {
 std::vector<forwarded_class> v = {};
    // std::vector<forwarded_class> v; // this one works
};

class forwarded_class {};
```
The error is 
```
In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-15.2.0/lib/gcc/x86_64-linux-gnu/15.2.0/../../../../include/c++/15.2.0/vector:68:
/opt/compiler-explorer/gcc-15.2.0/lib/gcc/x86_64-linux-gnu/15.2.0/../../../../include/c++/15.2.0/bits/stl_vector.h:376:35: error: arithmetic on a pointer to an incomplete type 'forwarded_class'
  376 |                       _M_impl._M_end_of_storage - _M_impl._M_start);
 |                       ~~~~~~~~~~~~~~~~~~~~~~~~~ ^
/opt/compiler-explorer/gcc-15.2.0/lib/gcc/x86_64-linux-gnu/15.2.0/../../../../include/c++/15.2.0/bits/stl_vector.h:561:7: note: in instantiation of member function 'std::_Vector_base<forwarded_class, std::allocator<forwarded_class>>::~_Vector_base' requested here
  561 | vector() = default;
      |       ^
<source>:6:36: note: in defaulted default constructor for 'std::vector<forwarded_class>' first required here
 6 |     std::vector<forwarded_class> v = {};
      | ^
<source>:3:7: note: forward declaration of 'forwarded_class'
    3 | class forwarded_class;
      |       ^
1 error generated.
Compiler returned: 1
```
https://godbolt.org/z/qMv9q5zxd


This code compiles with gcc 15.2 and msvc19.43
It does not compile with clang 21.1 or current trunk (4d0b81616975c4ea8984170122a04553cb14337a)

I thought those two declarations should be the same, since the vector should be default initialized anyway.


One difference I notice is how they interplay with `-Wmissing-designated-field-initializers`

Where having the extra `{}` seems to not require me to specify that field to not receive the warning
```cpp
#include <vector>
class forwarded_class {}; // changed to make it compile and show the warning

struct foo1 {
    int x;
 std::vector<forwarded_class> v = {};
};

struct foo2 {
    int x;
 std::vector<forwarded_class> v;
};

void x() {
    foo1 f{.x=0};
 foo2 g{.x=0};
}
```
```
<source>:17:16: warning: missing field 'v' initializer [-Wmissing-designated-field-initializers]
   17 |     foo2 g{.x=0};
      | ^
```
https://godbolt.org/z/7zYeaa68M



_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to