[PATCH v4 1/2] libstdc++: Implement constant_wrapper, cw from P2781R9.

Luc Grosheintz luc.grosheintz@gmail.com
Tue Sep 9 12:18:31 GMT 2025



On 9/9/25 2:15 PM, Tomasz Kaminski wrote:
> On Tue, Sep 9, 2025 at 12:45 PM Jonathan Wakely <jwakely@redhat.com> wrote:
> 
>> On Fri, 5 Sept 2025 at 08:14, Tomasz Kamiński <tkaminsk@redhat.com> wrote:
>>>
>>> From: Luc Grosheintz <luc.grosheintz@gmail.com>
>>>
>>> This is a partial implementation of P2781R9. It adds std::cw and
>>> std::constant_wrapper, but doesn't modify __integral_constant_like for
>>> span/mdspan.
>>>
>>> libstdc++-v3/ChangeLog:
>>>
>>>          * include/bits/version.def (constant_wrapper): Add.
>>>          * include/bits/version.h: Regenerate.
>>>          * include/std/type_traits (_CwFixedValue): New class.
>>>          (_IndexSequence): New struct.
>>>          (_BuildIndexSequence): New struct.
>>>          (_ConstExprParam): New concept.
>>>          (_CwOperators): New struct.
>>>          (constant_wrapper): New struct.
>>>          (cw): New global constant.
>>>          * src/c++23/std.cc.in (constant_wrapper): Add.
>>>          (cw): Add.
>>>          * testsuite/20_util/constant_wrapper/adl.cc: New test.
>>>          * testsuite/20_util/constant_wrapper/ex.cc: New test.
>>>          * testsuite/20_util/constant_wrapper/generic.cc: New test.
>>>          * testsuite/20_util/constant_wrapper/instantiate.cc: New test.
>>>          * testsuite/20_util/constant_wrapper/op_comma_neg.cc: New test.
>>>          * testsuite/20_util/constant_wrapper/version.cc: New test.
>>>
>>> Co-authored-by: Tomasz Kamiński <tkaminsk@redhat.com>
>>> Signed-off-by: Luc Grosheintz <luc.grosheintz@gmail.com>
>>> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
>>> ---
>>> v5 adds data member pointer tests
>>
>> Some late comments that I should have noticed before approving it, sorry!
>>
>>
>>> diff --git a/libstdc++-v3/include/std/type_traits
>> b/libstdc++-v3/include/std/type_traits
>>> index 4636457eb5a..26cbbb4fd5b 100644
>>> --- a/libstdc++-v3/include/std/type_traits
>>> +++ b/libstdc++-v3/include/std/type_traits
>>> @@ -41,6 +41,7 @@
>>>
>>>   #define __glibcxx_want_bool_constant
>>>   #define __glibcxx_want_bounded_array_traits
>>> +#define __glibcxx_want_constant_wrapper
>>>   #define __glibcxx_want_has_unique_object_representations
>>>   #define __glibcxx_want_integral_constant_callable
>>>   #define __glibcxx_want_is_aggregate
>>> @@ -4302,6 +4303,376 @@ template<typename _Ret, typename _Fn,
>> typename... _Args>
>>>       };
>>>   #endif // C++11
>>>
>>> +#ifdef __cpp_lib_constant_wrapper // C++ >= 26
>>> +  template<typename _Tp>
>>> +    struct _CwFixedValue
>>> +    {
>>> +      using _S_type = _Tp;
>>
>> The "_S_" prefix is for static members, this should be just "__type".
>> I'll change this.
>>
>>
>>> +
>>> +  template<_CwFixedValue _Tp,
>>> +          typename = typename decltype(_CwFixedValue(_Tp))::_S_type>
>>> +    struct constant_wrapper;
>>
>> Why do we need decltype(_CwFixedValue(_Tp)) here?
>>
>> It looks to me like just decltype(_Tp)::_S_type should work ... but it
>> doesn't. Is that something to do with CTAD being involved?
>>
> I think this is a GCC bug, that got a workaround backed in the standard
> (the standard specifies the argument this way, this is why it haven't
> picked up in review). Just decltype(_Tp) works in clang.

This ran a bell:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117392

> 
> 
>>
>>> +
>>> +  template<_CwFixedValue _X, typename>
>>> +  struct constant_wrapper : _CwOperators
>>
>> _X is (or was, at some point) used by libc headers on at least one
>> UNIX variant. I've already pushed a change to make that _Xv instead.
>> This is the same reason that we use _Tp instead of _T.
>>
>> I think I'll change the _Tp in the constant_wrapper declaration to
>> match _Xv, since it's not a type, it's a constant template parameter
>> (what used to be called non-type template parameter until
>> https://github.com/cplusplus/draft/pull/7587 renamed it).
>>
>>
> 



More information about the Libstdc++ mailing list