Title
`bitset(const CharT*)` constructor needs to be constrained
Status
new
Section
[bitset.cons]
Submitter
Jonathan Wakely

Created on 2025-07-12.00:00:00 last changed 6 days ago

Messages

Date: 2025-07-15.10:48:35

Proposed resolution:

This wording is relative to N5008.

  1. Modify [bitset.cons] as indicated:

    
    template<class charT>
      constexpr explicit bitset(
        const charT* str,
        typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos,
        charT zero = charT(’0’),
        charT one = charT(’1’));
    

    -?- Constraints:

    • is_array_v<charT> is `false`,
    • is_trivially_copyable_v<charT> is `true`,
    • is_standard_layout_v<charT> is `true`, and
    • is_trivially_default_constructible_v<charT> is `true`.

    -8- Effects: As if by:

    bitset(n == basic_string_view<charT>::npos
              ? basic_string_view<charT>(str)
              : basic_string_view<charT>(str, n),
           0, n, zero, one)
    

Date: 2025-07-14.19:23:15

This code might be ill-formed, with an error outside the immediate context that users cannot prevent:


#include <bitset>
struct NonTrivial { ~NonTrivial() { } };
static_assert( ! std::is_constructible_v<std::bitset<1>, NonTrivial*> );

The problem is that the `bitset(const CharT*)` constructor tries to instantiate basic_string_view<NonTrivial> to find its `size_type`, and that instantiation might ill-formed, e.g. if `std::basic_string_view` or `std::char_traits` has a static assert enforcing the requirement for their character type to be sufficiently char-like. [strings.general] defines a char-like type as "any non-array trivially copyable standard-layout ([basic.types.general]) type `T` where is_trivially_default_constructible_v<T> is `true`."

History
Date User Action Args
2025-07-12 11:21:41adminsetmessages: + msg14895
2025-07-12 00:00:00admincreate