Following code fails to compile on gcc (GCC) 13.2.1 20230801 but on clang (16.0.6 or 17.0.1). ``` #include <vector> struct A { std::vector<int> vec; int a; }; consteval auto foo() { const A tmp1{{1, 2}, 42}; } int main() { foo(); return 0; } ``` With following error message: ``` > g++ -Wall -Wextra -std=c++20 main.cpp In file included from /usr/include/c++/13.2.1/vector:66, from main2.cpp:1: /usr/include/c++/13.2.1/bits/stl_vector.h: In function ‘int main()’: main2.cpp:11:6: in ‘constexpr’ expansion of ‘foo()’ main2.cpp:8:49: in ‘constexpr’ expansion of ‘std::vector<int>(std::initializer_list<int>{((const int*)(& const int [2]{1, 2})), 2}, ((const std::vector<int>::allocator_type&)(& std::allocator<int>())))’ /usr/include/c++/13.2.1/bits/stl_vector.h:679:21: in ‘constexpr’ expansion of ‘((std::vector<int>*)this)->std::vector<int>::_M_range_initialize<const int*>(__l.std::initializer_list<int>::begin(), __l.std::initializer_list<int>::end(), (std::random_access_iterator_tag{std::bidirectional_iterator_tag()}, std::forward_iterator_tag()))’ /usr/include/c++/13.2.1/bits/stl_vector.h:1689:13: error: modifying a const object ‘((std::vector<int>*)this)->std::vector<int>::<anonymous>.std::_Vector_base<int, std::allocator<int> >::_M_impl.std::_Vector_base<int, std::allocator<int> >::_Vector_impl::<anonymous>.std::_Vector_base<int, std::allocator<int> >::_Vector_impl_data::_M_start’ is not allowed in a constant expression 1688 | this->_M_impl._M_start | ~~~~~~~~~~~~~~~~~~~~~~ 1689 | = this->_M_allocate(_S_check_init_len(__n, _M_get_Tp_allocator())); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main2.cpp:8:32: note: originally declared ‘const’ here 8 | consteval auto foo() { const A tmp1{ {1, 2}, 42 }; } | ^~~~ ``` If member variable `a` is removed it compiles: ``` #include <vector> struct A { std::vector<int> vec; }; consteval auto foo() { const A tmp1{ {1, 2} }; } int main() { foo(); return 0; } ```
Confirmed. We should reduce this to remove the include file too ...
(In reply to Andrew Pinski from comment #1) > Confirmed. We should reduce this to remove the include file too ... Here is somewhat reduced: ``` #include <memory> auto alloc = std::allocator<int>{}; constexpr auto n = 3; struct vec { int* data; constexpr vec() { this->data = alloc.allocate(n); } constexpr ~vec() { alloc.deallocate(this->data, n); } }; struct A { vec v; int a; }; consteval auto foo() { A temp1{{}, 42}; // This compiles fine const A tmp2{{}, 42}; // This does not } int main() { foo(); } ```
Here is even more reduced. Notice that if the unused data member `pointer` is removed from `vec` the problem goes away. Same with data member `a` in struct `A`. ``` struct vec { int data; int* pointer; // This is required for compilation to fail constexpr vec() { data = 42; } }; struct A { vec v; int a; // This is required for compilation to fail }; consteval auto foo() { A temp1{{}, 42}; // This compiles fine const A tmp2{{}, 42}; // This does not } int main() { foo(); } ```
Looks like it is the uninitialized field pointer which is causing the issues. Looks like GCC does not currently fully support that for const variables inside a constexpr/consteval . The linked PRs are about string but I think the same issue shows up.