From d26c0294732639c09dc86d3f7788af8ebb8131fc Mon Sep 17 00:00:00 2001 From: "A. Jiang" Date: Fri, 14 Nov 2025 18:23:13 +0800 Subject: [PATCH] Implement LWG-4243 --- stl/inc/span | 3 ++- tests/std/tests/P0122R7_span/test.cpp | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/stl/inc/span b/stl/inc/span index e1f58060c8a..97fe33189c4 100644 --- a/stl/inc/span +++ b/stl/inc/span @@ -558,13 +558,14 @@ span(_Rng&&) -> span>>; #ifdef __cpp_lib_byte // [span.objectrep] Views of object representation _EXPORT_STD template + requires (!is_volatile_v<_Ty>) _NODISCARD auto as_bytes(span<_Ty, _Extent> _Sp) noexcept { using _ReturnType = span; return _ReturnType{reinterpret_cast(_Sp.data()), _Sp.size_bytes()}; } _EXPORT_STD template - requires (!is_const_v<_Ty>) + requires (!is_const_v<_Ty> && !is_volatile_v<_Ty>) _NODISCARD auto as_writable_bytes(span<_Ty, _Extent> _Sp) noexcept { using _ReturnType = span; return _ReturnType{reinterpret_cast(_Sp.data()), _Sp.size_bytes()}; diff --git a/tests/std/tests/P0122R7_span/test.cpp b/tests/std/tests/P0122R7_span/test.cpp index 939a832d954..5b50d73a9cb 100644 --- a/tests/std/tests/P0122R7_span/test.cpp +++ b/tests/std/tests/P0122R7_span/test.cpp @@ -153,11 +153,11 @@ using BorrowedContiguousSizedRange = BasicRange; template constexpr void FunctionTakingSpan(type_identity_t>) {} -template -constexpr bool AsWritableBytesCompilesFor = false; +template +constexpr bool AsBytesCompilesFor = requires { as_bytes(declval()); }; template -constexpr bool AsWritableBytesCompilesFor()))>> = true; +constexpr bool AsWritableBytesCompilesFor = requires { as_writable_bytes(declval()); }; constexpr bool test() { { @@ -1016,10 +1016,23 @@ void test_non_constexpr() { static_assert(noexcept(as_writable_bytes(sp_dyn))); static_assert(noexcept(as_writable_bytes(sp_nine))); + static_assert(AsBytesCompilesFor>); + static_assert(AsBytesCompilesFor>); + static_assert(AsBytesCompilesFor>); + static_assert(AsBytesCompilesFor>); + static_assert(!AsBytesCompilesFor>); + static_assert(!AsBytesCompilesFor>); + static_assert(!AsBytesCompilesFor>); + static_assert(!AsBytesCompilesFor>); + static_assert(AsWritableBytesCompilesFor>); static_assert(AsWritableBytesCompilesFor>); static_assert(!AsWritableBytesCompilesFor>); - static_assert(!AsWritableBytesCompilesFor>); + static_assert(!AsWritableBytesCompilesFor>); + static_assert(!AsWritableBytesCompilesFor>); + static_assert(!AsWritableBytesCompilesFor>); + static_assert(!AsWritableBytesCompilesFor>); + static_assert(!AsWritableBytesCompilesFor>); auto sp_1 = as_bytes(sp_dyn); auto sp_2 = as_bytes(sp_nine);