[PATCH] libstdc++: constexpr flat_set and flat_multiset
Jonathan Wakely
jwakely@redhat.com
Tue Dec 2 09:20:16 GMT 2025
On Tue, 2 Dec 2025 at 08:16, Tomasz Kaminski <tkaminsk@redhat.com> wrote:
>
>
>
> On Mon, Dec 1, 2025 at 8:51 PM Patrick Palka <ppalka@redhat.com> wrote:
>>
>> On Wed, 26 Nov 2025, Yuao Ma wrote:
>>
>> > Hello world,
>> >
>> > This patch adds constexpr support to flat_map and flat_multiset in
>> > accordance with P3372R3. I have also added test cases to verify the
>> > new constexpr functionality.
>> > Tested on x86_64-linux.
>> > Please take a look when you have time. Thanks!
>>
>> Thanks for the patch!
>>
>> > Subject: [PATCH] libstdc++: constexpr flat_set and flat_multiset
>> >
>> > This patch makes flat_map and flat_multiset constexpr as part of P3372R3.
>>
>> s/flat_map/flat_set
>>
>> >
>> > libstdc++-v3/ChangeLog:
>> >
>> > * include/bits/version.def: Add FTM.
>> > * include/bits/version.h: Regenerate.
>> > * include/std/flat_set: Add constexpr.
>> > * testsuite/23_containers/flat_multiset/constexpr.cc: New test.
>> > * testsuite/23_containers/flat_set/constexpr.cc: New test.
>> > ---
>> > libstdc++-v3/include/bits/version.def | 9 +
>> > libstdc++-v3/include/bits/version.h | 10 +
>> > libstdc++-v3/include/std/flat_set | 103 ++++++-
>> > .../23_containers/flat_multiset/constexpr.cc | 239 +++++++++++++++++
>> > .../23_containers/flat_set/constexpr.cc | 253 ++++++++++++++++++
>> > 5 files changed, 611 insertions(+), 3 deletions(-)
>> > create mode 100644 libstdc++-v3/testsuite/23_containers/flat_multiset/constexpr.cc
>> > create mode 100644 libstdc++-v3/testsuite/23_containers/flat_set/constexpr.cc
>> >
>> > diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def
>> > index 1fde9eef9d3..93965e914d5 100644
>> > --- a/libstdc++-v3/include/bits/version.def
>> > +++ b/libstdc++-v3/include/bits/version.def
>> > @@ -1347,6 +1347,15 @@ ftms = {
>> > };
>> > };
>> >
>> > +ftms = {
>> > + name = constexpr_flat_set;
>> > + values = {
>> > + v = 202502;
>> > + cxxmin = 26;
>> > + hosted = yes;
>>
>> why hosted? flat_set isn't hosted IIRC
>>
>> What do people think of supporting this as an extension in C++23 mode?
>> That way we could just specify constexpr instead of the ugly
>> _GLIBCXX26_CONSTEXPR.
>
> This will lead to non-portable code, and while this is mostly a loud change
> (i.e. code does not compile), the fact that following variable (as global) have static
> vs dynamic initialization may be important:
> flat_map<int, float> mapping = ....;
> And may work correctly on libstdc++ in C++23 mode, while having order of initialization
> issues in other compilers.
>
> So, I would preffer not having constexpr as an extension. Technically, I do not think
> that adding constexpr can be done as extension, as we may change meaning of valid
> C++20 program, but now taking a different if consteval branch.
How about it we made all members constexpr *except* the constructors
and destructor, which would use _GLIBCXX23_CONSTEXPR?
I think that would avoid the portability problems (you couldn't create
one during constant-initialization or during constant-evaluation) but
would avoid the ugly CONSTEXPR macros on the other members.
More information about the Libstdc++
mailing list