|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4294" status="New"> |
| 5 | +<title>`bitset(const CharT*)` constructor needs to be constrained</title> |
| 6 | +<section><sref ref="[bitset.cons]"/></section> |
| 7 | +<submitter>Jonathan Wakely</submitter> |
| 8 | +<date>12 Jul 2025</date> |
| 9 | +<priority>99</priority> |
| 10 | + |
| 11 | +<discussion> |
| 12 | +<p> |
| 13 | +This code might be ill-formed, with an error outside the immediate context |
| 14 | +that users cannot prevent: |
| 15 | +<blockquote><pre><code> |
| 16 | +#include <bitset> |
| 17 | +struct NonTrivial { ~NonTrivial() { } }; |
| 18 | +static_assert( ! std::is_constructible_v<std::bitset<1>, NonTrivial*> ); |
| 19 | +</code></pre></blockquote> |
| 20 | +</p> |
| 21 | +<p> |
| 22 | +The problem is that the `bitset(const CharT*)` constructor tries to instantiate |
| 23 | +<code>basic_string_view<NonTrivial></code> to find its `size_type`, |
| 24 | +and that instantiation might ill-formed (e.g. if `std::basic_string_view` or |
| 25 | +`std::char_traits` has a static assert enforcing the requirement for their |
| 26 | +character type to be sufficiently char-like (<sref ref="[strings.general]"/>). |
| 27 | +</p> |
| 28 | +</discussion> |
| 29 | + |
| 30 | +<resolution> |
| 31 | +<p> |
| 32 | +This wording is relative to <paper num="N5008"/>. |
| 33 | +</p> |
| 34 | + |
| 35 | +<ol> |
| 36 | + |
| 37 | +<li><p>Modify <sref ref="[bitset.cons]"/> as indicated:</p> |
| 38 | + |
| 39 | +<blockquote> |
| 40 | +<pre><code> |
| 41 | +template<class charT> |
| 42 | + constexpr explicit bitset( |
| 43 | + const charT* str, |
| 44 | + typename basic_string_view<charT>::size_type n = basic_string_view<charT>::npos, |
| 45 | + charT zero = charT(’0’), |
| 46 | + charT one = charT(’1’)); |
| 47 | +</code></pre> |
| 48 | +<blockquote> |
| 49 | +<p> |
| 50 | +<ins>-?- <i>Constraints</i>: |
| 51 | +<code>is_trivially_default_constructible_v<charT></code> is `true`. |
| 52 | +</ins> |
| 53 | +</p> |
| 54 | +<p>-8- <i>Effects</i>: As if by: |
| 55 | +<blockquote> |
| 56 | +<pre><code>bitset(n == basic_string_view<charT>::npos |
| 57 | + ? basic_string_view<charT>(str) |
| 58 | + : basic_string_view<charT>(str, n), |
| 59 | + 0, n, zero, one) |
| 60 | +</code></pre> |
| 61 | +</blockquote> |
| 62 | +</p> |
| 63 | +</blockquote> |
| 64 | +</blockquote> |
| 65 | +</li> |
| 66 | +</ol> |
| 67 | + |
| 68 | + |
| 69 | +</resolution> |
| 70 | + |
| 71 | +</issue> |
0 commit comments