public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113021] New: [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member
@ 2023-12-14 14:25 miro.palmu at helsinki dot fi
2023-12-14 17:35 ` [Bug c++/113021] " pinskia at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: miro.palmu at helsinki dot fi @ 2023-12-14 14:25 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113021
Bug ID: 113021
Summary: [constexpr] gcc rejects initializing struct containing
vector during constant evaluation depending if the
struct also contains other member
Product: gcc
Version: 13.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: miro.palmu at helsinki dot fi
Target Milestone: ---
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;
}
```
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/113021] [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member
2023-12-14 14:25 [Bug c++/113021] New: [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member miro.palmu at helsinki dot fi
@ 2023-12-14 17:35 ` pinskia at gcc dot gnu.org
2023-12-15 10:44 ` miro.palmu at helsinki dot fi
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-14 17:35 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113021
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Last reconfirmed| |2023-12-14
Status|UNCONFIRMED |NEW
Keywords| |needs-reduction
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. We should reduce this to remove the include file too ...
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/113021] [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member
2023-12-14 14:25 [Bug c++/113021] New: [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member miro.palmu at helsinki dot fi
2023-12-14 17:35 ` [Bug c++/113021] " pinskia at gcc dot gnu.org
@ 2023-12-15 10:44 ` miro.palmu at helsinki dot fi
2023-12-15 10:55 ` miro.palmu at helsinki dot fi
2023-12-15 19:40 ` pinskia at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: miro.palmu at helsinki dot fi @ 2023-12-15 10:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113021
--- Comment #2 from Miro Palmu <miro.palmu at helsinki dot fi> ---
(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();
}
```
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/113021] [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member
2023-12-14 14:25 [Bug c++/113021] New: [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member miro.palmu at helsinki dot fi
2023-12-14 17:35 ` [Bug c++/113021] " pinskia at gcc dot gnu.org
2023-12-15 10:44 ` miro.palmu at helsinki dot fi
@ 2023-12-15 10:55 ` miro.palmu at helsinki dot fi
2023-12-15 19:40 ` pinskia at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: miro.palmu at helsinki dot fi @ 2023-12-15 10:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113021
--- Comment #3 from Miro Palmu <miro.palmu at helsinki dot fi> ---
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();
}
```
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/113021] [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member
2023-12-14 14:25 [Bug c++/113021] New: [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member miro.palmu at helsinki dot fi
` (2 preceding siblings ...)
2023-12-15 10:55 ` miro.palmu at helsinki dot fi
@ 2023-12-15 19:40 ` pinskia at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-15 19:40 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113021
--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
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.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-12-15 19:40 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-14 14:25 [Bug c++/113021] New: [constexpr] gcc rejects initializing struct containing vector during constant evaluation depending if the struct also contains other member miro.palmu at helsinki dot fi
2023-12-14 17:35 ` [Bug c++/113021] " pinskia at gcc dot gnu.org
2023-12-15 10:44 ` miro.palmu at helsinki dot fi
2023-12-15 10:55 ` miro.palmu at helsinki dot fi
2023-12-15 19:40 ` pinskia at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).