Issue 4294: bitset(const CharT*) constructor needs to be constrained (original) (raw)


This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of WP status.

4294. bitset(const CharT*) constructor needs to be constrained

Section: 22.9.2.2 [bitset.cons] Status: WP Submitter: Jonathan Wakely Opened: 2025-07-12 Last modified: 2025-11-11

Priority: Not Prioritized

View all other issues in [bitset.cons].

View all issues with WP status.

Discussion:

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 instantiatebasic_string_view<NonTrivial> to find its size_type, and that instantiation might ill-formed, e.g. if std::basic_string_view orstd::char_traits has a static assert enforcing the requirement for their character type to be sufficiently char-like. 27.1 [strings.general]defines a char-like type as "any non-array trivially copyable standard-layout (6.9.1 [basic.types.general]) type Twhere is_trivially_default_constructible_v<T> is true."

[2025-08-21; Reflector poll]

Set status to Tentatively Ready after five votes in favour during reflector poll.

[Kona 2025-11-08; Status changed: Voting → WP.]

Proposed resolution:

This wording is relative to N5008.

  1. Modify 22.9.2.2 [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::npos
? basic_string_view(str)
: basic_string_view(str, n),
0, n, zero, one)

```