diff --git a/include/beman/utf_view/detail/constant_wrapper_polyfill.hpp b/include/beman/utf_view/detail/constant_wrapper_polyfill.hpp new file mode 100644 index 0000000..394e6dc --- /dev/null +++ b/include/beman/utf_view/detail/constant_wrapper_polyfill.hpp @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: BSL-1.0 + +// Copyright Eddie Nolan and Jonathan Wakely 2023 - 2025. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE.txt or copy at +// https://www.boost.org/LICENSE_1_0.txt) + +#ifndef BEMAN_UTF_VIEW_CONSTANT_WRAPPER_POLYFILL_HPP +#define BEMAN_UTF_VIEW_CONSTANT_WRAPPER_POLYFILL_HPP + +#include +#include + +namespace beman::utf_view::detail { + +#ifdef __cpp_lib_constant_wrapper + +template +using constant_wrapper = std::constant_wrapper; + +template constexpr auto cw{std::cw}; + +#else + +template +struct constant_wrapper { + static constexpr const auto value = X; +}; + +template constexpr auto cw{constant_wrapper{}}; + +#endif + + +} // namespace beman::utf_view::detail + +#endif // BEMAN_UTF_VIEW_CONSTANT_WRAPPER_POLYFILL_HPP diff --git a/include/beman/utf_view/detail/nontype_t_polyfill.hpp b/include/beman/utf_view/detail/nontype_t_polyfill.hpp deleted file mode 100644 index 5b57567..0000000 --- a/include/beman/utf_view/detail/nontype_t_polyfill.hpp +++ /dev/null @@ -1,37 +0,0 @@ -// SPDX-License-Identifier: BSL-1.0 - -// Copyright Eddie Nolan and Jonathan Wakely 2023 - 2025. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE.txt or copy at -// https://www.boost.org/LICENSE_1_0.txt) - -#ifndef BEMAN_UTF_VIEW_NONTYPE_T_POLYFILL_HPP -#define BEMAN_UTF_VIEW_NONTYPE_T_POLYFILL_HPP - -#include -#include - -namespace beman::utf_view::detail { - -#ifdef __cpp_lib_function_ref - -template -using nontype_t = std::nontype_t; - -template constexpr auto nontype{std::nontype}; - -#else - -template -struct nontype_t { - explicit nontype_t() = default; -}; - -template constexpr nontype_t nontype{}; - -#endif - - -} // namespace beman::utf_view::detail - -#endif // BEMAN_UTF_VIEW_NONTYPE_T_POLYFILL_HPP diff --git a/include/beman/utf_view/to_utf_view.hpp b/include/beman/utf_view/to_utf_view.hpp index d803016..e1a6d40 100644 --- a/include/beman/utf_view/to_utf_view.hpp +++ b/include/beman/utf_view/to_utf_view.hpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include @@ -131,9 +131,13 @@ class to_utf_view : public std::ranges::view_interface constexpr to_utf_view() requires std::default_initializable = default; - /* PAPER: constexpr explicit to_utf_view(V base, nontype_t, to_utf_tag_t); */ + /* PAPER: template */ + /* PAPER: constexpr explicit to_utf_view(V base, constant_wrapper, to_utf_tag_t) */ + /* PAPER: requires (constant_wrapper::value == E); */ /* !PAPER */ - constexpr explicit to_utf_view(V base, detail::nontype_t, to_utf_tag_t) + template + constexpr explicit to_utf_view(V base, detail::constant_wrapper, to_utf_tag_t) + requires (detail::constant_wrapper::value == E) : base_(std::move(base)) { } /* PAPER */ @@ -202,8 +206,8 @@ class to_utf_view : public std::ranges::view_interface /* PAPER */ }; -template -to_utf_view(R&&, detail::nontype_t, to_utf_tag_t) -> to_utf_view, E, ToType>; +template +to_utf_view(R&&, detail::constant_wrapper, to_utf_tag_t) -> to_utf_view, detail::constant_wrapper::value, ToType>; template requires std::ranges::view && exposition_only_code_unit> @@ -884,14 +888,14 @@ namespace detail { return std::ranges::empty_view>{}; } } else if constexpr (detail::is_to_utf_view_v) { - return to_utf_view(std::forward(r).base(), detail::nontype, to_utf_tag); + return to_utf_view(std::forward(r).base(), detail::cw, to_utf_tag); } else if constexpr (detail::is_to_utf_subrange_v) { return to_utf_view( std::ranges::subrange(r.begin().base(), r.end().base()), - detail::nontype, + detail::cw, to_utf_tag); - } else { - return to_utf_view(std::forward(r), detail::nontype, to_utf_tag); + } else { + return to_utf_view(std::forward(r), detail::cw, to_utf_tag); } } }; diff --git a/papers/P2728.md b/papers/P2728.md index 7ed572b..9bf4b4a 100644 --- a/papers/P2728.md +++ b/papers/P2728.md @@ -496,14 +496,14 @@ expression-equivalent to: - Otherwise, `empty_view>{}`. - Otherwise, if the type of `E` is a (possibly cv-qualified) specialization of - `to_utf_view`, then `to_utf_view(E.base(), nontype, to_utf_tag)`. + `to_utf_view`, then `to_utf_view(E.base(), cw, to_utf_tag)`. - Otherwise, if the type of `E` is *cv* `subrange` for some specialization of `to_utf_view`, then `to_utf_view(subrange(E.begin().base(), E.end().base()), - nontype, to_utf_tag)`. + cw, to_utf_tag)`. -- Otherwise, `to_utf_view(E, nontype, to_utf_tag)`. +- Otherwise, `to_utf_view(E, cw, to_utf_tag)`. #### 24.7.?.2 Enumeration `utf_transcoding_error` [range.transcoding.error.transcoding] {-} @@ -569,7 +569,9 @@ private: public: constexpr to_utf_view() requires default_initializable = default; - constexpr explicit to_utf_view(V base, nontype_t, to_utf_tag_t); + template + constexpr explicit to_utf_view(V base, constant_wrapper, to_utf_tag_t) + requires (constant_wrapper::value == E); constexpr V base() const& requires copy_constructible { return @*base_*@; } constexpr V base() && { return std::move(@*base_*@); } @@ -607,12 +609,15 @@ public: constexpr auto reserve_hint() const requires approximately_sized_range; }; -template - to_utf_view(R&&, nontype_t, to_utf_tag_t) -> to_utf_view, E, ToType>; +template + to_utf_view(R&&, constant_wrapper, to_utf_tag_t) + -> to_utf_view, constant_wrapper::value, ToType>; ``` ```cpp -constexpr explicit to_utf_view(V base, nontype_t, to_utf_tag_t); +template + constexpr explicit to_utf_view(V base, constant_wrapper, to_utf_tag_t) + requires (constant_wrapper::value == E); ``` _Effects_: Initializes `@*base_*@` with `std::move(base)`. @@ -1067,6 +1072,10 @@ gives back the original underlying view if it detects that it's reversing anothe # Changelog +## Changes since R11 + +- Refactor use of `std::nontype` to use `std::constant_wrapper` instead per P3948. + ## Changes since R10 - Fix the wording around rejecting arrays for the code unit adaptors diff --git a/src/beman/utf_view/CMakeLists.txt b/src/beman/utf_view/CMakeLists.txt index 87e493d..379728a 100644 --- a/src/beman/utf_view/CMakeLists.txt +++ b/src/beman/utf_view/CMakeLists.txt @@ -22,9 +22,9 @@ target_sources( FILES ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/beman/utf_view/code_unit_view.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/beman/utf_view/detail/concepts.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/beman/utf_view/detail/constant_wrapper_polyfill.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/beman/utf_view/detail/constexpr_unless_msvc.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/beman/utf_view/detail/fake_inplace_vector.hpp - ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/beman/utf_view/detail/nontype_t_polyfill.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/beman/utf_view/endian_view.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/beman/utf_view/null_term.hpp ${CMAKE_CURRENT_SOURCE_DIR}/../../../include/beman/utf_view/to_utf_view.hpp diff --git a/tests/beman/utf_view/to_utf_view.t.cpp b/tests/beman/utf_view/to_utf_view.t.cpp index ed88703..ee138c4 100644 --- a/tests/beman/utf_view/to_utf_view.t.cpp +++ b/tests/beman/utf_view/to_utf_view.t.cpp @@ -5,6 +5,7 @@ // (See accompanying file LICENSE.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) +#include #include #include #include @@ -871,7 +872,7 @@ constexpr bool empty_test() { std::is_same_v< decltype(std::views::empty | to_utf8_or_error), std::ranges::empty_view>>); - auto empty_utf{to_utf_view{std::views::empty, detail::nontype, to_utf8_tag}}; + auto empty_utf{to_utf_view{std::views::empty, detail::cw, to_utf8_tag}}; if (!empty_utf.empty()) { return false; }