|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4382" status="New"> |
| 5 | +<title>The <tt>simd::basic_mask(bool)</tt> overload needs to be more constrained |
| 6 | +</title> |
| 7 | +<section> |
| 8 | +<sref ref="[simd.mask.ctor]"/> |
| 9 | +</section> |
| 10 | +<submitter>Matthias Kretz</submitter> |
| 11 | +<date>24 Sep 2025</date> |
| 12 | +<priority>99</priority> |
| 13 | + |
| 14 | +<discussion> |
| 15 | +<p> |
| 16 | +<sref ref="[simd.mask.ctor]"/> defines the overloads `basic_mask(bool)` and |
| 17 | +`basic_mask(unsigned_integral auto)`. This leads to the following pitfall: |
| 18 | +</p> |
| 19 | +<blockquote><pre> |
| 20 | +auto g0() { |
| 21 | + unsigned short k = 0xf; |
| 22 | + return simd::mask<float, 8>(k); // mov eax, 15 |
| 23 | +} |
| 24 | + |
| 25 | +auto g1() { |
| 26 | + unsigned short k = 0xf; |
| 27 | + return simd::mask<float, 8>(k >> 1); // mov eax, -1 ⚠️ |
| 28 | +} |
| 29 | + |
| 30 | +auto g2() { |
| 31 | + unsigned int k = 0xf; |
| 32 | + return simd::mask<float, 8>(k >> 1); // mov eax, 7 |
| 33 | +} |
| 34 | +</pre></blockquote> |
| 35 | +<p> |
| 36 | +In `g1`, `k` is promoted to `int`, shifted and then passed to |
| 37 | +the mask constructor. Instead of failing, `int(0x7)` is |
| 38 | +converted to `bool` and the mask thus initialized to all `true`. |
| 39 | +<p/> |
| 40 | +Also consider: |
| 41 | +</p> |
| 42 | +<ol> |
| 43 | +<li><p><tt>simd::mask<float>(true_type());</tt></p></li> |
| 44 | +<li><p><tt>unsigned_integral<bool></tt> is `true` => |
| 45 | +<tt>same_as<bool> auto</tt> instead of 'bool' makes |
| 46 | +the overload set ambiguous</p></li> |
| 47 | +<li><p>`float` is convertible to `bool`, thus |
| 48 | +<tt>simd::mask<float>(1.f)</tt> continues to compile</p></li> |
| 49 | +</ol> |
| 50 | +</discussion> |
| 51 | + |
| 52 | +<resolution> |
| 53 | +<p> |
| 54 | +This wording is relative to <paper num="N5014"/>. |
| 55 | +</p> |
| 56 | + |
| 57 | +<ol> |
| 58 | + |
| 59 | +<li><p>Modify <sref ref="[simd.mask.overview]"/>, <tt>class template basic_mask</tt> synopsis, as indicated:</p> |
| 60 | + |
| 61 | +<blockquote> |
| 62 | +<pre> |
| 63 | +namespace std::simd { |
| 64 | + template<size_t Bytes, class Abi> class basic_mask { |
| 65 | + public: |
| 66 | + […] |
| 67 | + |
| 68 | + constexpr basic_mask() noexcept = default; |
| 69 | + |
| 70 | + // <i><sref ref="[simd.mask.ctor]"/>, basic_mask constructors</i> |
| 71 | + constexpr explicit basic_mask(value_type) noexcept; |
| 72 | + template<size_t UBytes, class UAbi> |
| 73 | + constexpr explicit basic_mask(const basic_mask<UBytes, UAbi>&) noexcept; |
| 74 | + template<class G> |
| 75 | + constexpr explicit basic_mask(G&& gen) noexcept; |
| 76 | + constexpr basic_mask(const bitset<size()>& b) noexcept; |
| 77 | + constexpr explicit basic_mask(unsigned_integral auto val) noexcept; |
| 78 | + <ins>basic_mask(signed_integral auto) = delete;</ins> |
| 79 | + |
| 80 | + […] |
| 81 | + }; |
| 82 | +} |
| 83 | +</pre> |
| 84 | +</blockquote> |
| 85 | + |
| 86 | +</li> |
| 87 | + |
| 88 | +</ol> |
| 89 | +</resolution> |
| 90 | + |
| 91 | +</issue> |
0 commit comments