|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4371" status="New"> |
| 5 | +<title>Container adaptor's `empty`/`size` should be `noexcept`</title> |
| 6 | +<section> |
| 7 | +<sref ref="[queue.defn]"/> |
| 8 | +<sref ref="[priqueue.overview]"/> |
| 9 | +<sref ref="[stack.defn]"/> |
| 10 | +</section> |
| 11 | +<submitter>Hewill Kang</submitter> |
| 12 | +<date>09 Sep 2025</date> |
| 13 | +<priority>99</priority> |
| 14 | + |
| 15 | +<discussion> |
| 16 | +<p> |
| 17 | +C++23 container adaptors <tt>flat_<i>meow</i></tt> all have `noexcept` `size`/`empty` members. |
| 18 | +<p/> |
| 19 | +However, the `size`/`empty` members of other container adaptors are not mark `noexcept`, |
| 20 | +even though they behave the same as <tt>flat_<i>meow</i></tt> that returning the `size`/`empty` |
| 21 | +of the underlying container. |
| 22 | +<p/> |
| 23 | +It makes sense to make them `noexcept` as well for consistency. Although the standard doesn't |
| 24 | +explicitly say those two members of the container must not throw, the fact that all standard |
| 25 | +containers and common third-party containers mark them as unconditionally `noexcept` implies |
| 26 | +that it's perfectly reasonable to assume that they never will. |
| 27 | +</p> |
| 28 | +</discussion> |
| 29 | + |
| 30 | +<resolution> |
| 31 | +<p> |
| 32 | +This wording is relative to <paper num="N5014"/>. |
| 33 | +</p> |
| 34 | + |
| 35 | +<ol> |
| 36 | + |
| 37 | +<li><p>Modify <sref ref="[queue.defn]"/> as indicated:</p> |
| 38 | + |
| 39 | +<blockquote> |
| 40 | +<pre> |
| 41 | +namespace std { |
| 42 | + template<class T, class Container = deque<T>> |
| 43 | + class queue { |
| 44 | + public: |
| 45 | + […] |
| 46 | + constexpr bool empty() const <ins>noexcept</ins> { return c.empty(); } |
| 47 | + constexpr size_type size() const <ins>noexcept</ins> { return c.size(); } |
| 48 | + […] |
| 49 | + }; |
| 50 | + […] |
| 51 | +} |
| 52 | +</pre> |
| 53 | +</blockquote> |
| 54 | + |
| 55 | +</li> |
| 56 | + |
| 57 | +<li><p>Modify <sref ref="[priqueue.overview]"/> as indicated:</p> |
| 58 | + |
| 59 | +<blockquote> |
| 60 | +<pre> |
| 61 | +namespace std { |
| 62 | + template<class T, class Container = vector<T>, |
| 63 | + class Compare = less<typename Container::value_type>> |
| 64 | + class priority_queue { |
| 65 | + public: |
| 66 | + […] |
| 67 | + constexpr bool empty() const <ins>noexcept</ins> { return c.empty(); } |
| 68 | + constexpr size_type size() const <ins>noexcept</ins> { return c.size(); } |
| 69 | + […] |
| 70 | + }; |
| 71 | + […] |
| 72 | +} |
| 73 | +</pre> |
| 74 | +</blockquote> |
| 75 | + |
| 76 | +</li> |
| 77 | + |
| 78 | +<li><p>Modify <sref ref="[stack.defn]"/> as indicated:</p> |
| 79 | + |
| 80 | +<blockquote> |
| 81 | +<pre> |
| 82 | +namespace std { |
| 83 | + template<class T, class Container = deque<T>> |
| 84 | + class stack { |
| 85 | + public: |
| 86 | + […] |
| 87 | + constexpr bool empty() const <ins>noexcept</ins> { return c.empty(); } |
| 88 | + constexpr size_type size() const <ins>noexcept</ins> { return c.size(); } |
| 89 | + […] |
| 90 | + }; |
| 91 | + […] |
| 92 | +} |
| 93 | +</pre> |
| 94 | +</blockquote> |
| 95 | + |
| 96 | +</li> |
| 97 | + |
| 98 | +</ol></resolution> |
| 99 | + |
| 100 | +</issue> |
0 commit comments