From 29f77ca7a08aab772f7ac40ca7f48e581fb01a08 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 27 May 2025 10:49:17 -0700 Subject: [PATCH 1/3] Avoid a compiler warning with Defect Report P2280R4 --- stl/inc/vector | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stl/inc/vector b/stl/inc/vector index b84dbda19d3..fb1ab20ed14 100644 --- a/stl/inc/vector +++ b/stl/inc/vector @@ -3193,8 +3193,11 @@ public: } _NODISCARD _CONSTEXPR20 size_type max_size() const noexcept { - constexpr auto _Diff_max = static_cast(_STD _Max_limit()); - const size_type _Ints_max = this->_Myvec.max_size(); + constexpr auto _Diff_max = static_cast(_STD _Max_limit()); + + // _Ints_max is non-const to avoid warning C4127 "conditional expression is constant" with Defect Report P2280R4 + size_type _Ints_max = this->_Myvec.max_size(); + if (_Ints_max > _Diff_max / _VBITS) { // max_size bound by difference_type limits return _Diff_max; } From 098730686e887f684681de489e4581cdb7602c45 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 27 May 2025 11:53:51 -0700 Subject: [PATCH 2/3] Use a `const bool` instead. --- stl/inc/vector | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/stl/inc/vector b/stl/inc/vector index fb1ab20ed14..f68011d3339 100644 --- a/stl/inc/vector +++ b/stl/inc/vector @@ -3193,12 +3193,12 @@ public: } _NODISCARD _CONSTEXPR20 size_type max_size() const noexcept { - constexpr auto _Diff_max = static_cast(_STD _Max_limit()); + constexpr auto _Diff_max = static_cast(_STD _Max_limit()); + const size_type _Ints_max = this->_Myvec.max_size(); - // _Ints_max is non-const to avoid warning C4127 "conditional expression is constant" with Defect Report P2280R4 - size_type _Ints_max = this->_Myvec.max_size(); - - if (_Ints_max > _Diff_max / _VBITS) { // max_size bound by difference_type limits + // Avoid warning C4127 "conditional expression is constant" with Defect Report P2280R4 + const bool _Would_overflow = _Ints_max > _Diff_max / _VBITS; + if (_Would_overflow) { // max_size bound by difference_type limits return _Diff_max; } From a306d2d6be466145800b7aeeae40bc1ca6169b88 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Tue, 27 May 2025 12:10:39 -0700 Subject: [PATCH 3/3] Expand comment slightly. --- stl/inc/vector | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stl/inc/vector b/stl/inc/vector index f68011d3339..bf7b9dd80fb 100644 --- a/stl/inc/vector +++ b/stl/inc/vector @@ -3196,7 +3196,7 @@ public: constexpr auto _Diff_max = static_cast(_STD _Max_limit()); const size_type _Ints_max = this->_Myvec.max_size(); - // Avoid warning C4127 "conditional expression is constant" with Defect Report P2280R4 + // Use a const bool to avoid warning C4127 "conditional expression is constant" with Defect Report P2280R4 const bool _Would_overflow = _Ints_max > _Diff_max / _VBITS; if (_Would_overflow) { // max_size bound by difference_type limits return _Diff_max;