diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2023-06-08 17:31:29 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2023-06-08 17:31:29 +0200 |
| commit | eb13bd9991ceef34cacb0913df90045b08726f81 (patch) | |
| tree | 46d0951d741e40fe76f27f13de0e8f10863b798e | |
| parent | 93e02a01568519344310897e8e9f4ee4f32ff75f (diff) | |
| download | newtype-eb13bd9991ceef34cacb0913df90045b08726f81.tar.xz newtype-eb13bd9991ceef34cacb0913df90045b08726f81.zip | |
concepts: replace is_*_output_streamable*
| -rw-r--r-- | source/lib/include/newtype/concepts.hpp | 15 | ||||
| -rw-r--r-- | source/lib/include/newtype/impl/type_traits_extensions.hpp | 33 | ||||
| -rw-r--r-- | source/lib/include/newtype/newtype.hpp | 10 | ||||
| -rw-r--r-- | source/tests/src/io_operators.cpp | 2 |
4 files changed, 22 insertions, 38 deletions
diff --git a/source/lib/include/newtype/concepts.hpp b/source/lib/include/newtype/concepts.hpp index c053612..c9ef951 100644 --- a/source/lib/include/newtype/concepts.hpp +++ b/source/lib/include/newtype/concepts.hpp @@ -54,6 +54,21 @@ namespace nt::concepts } noexcept; }; + template<typename SubjectType, typename CharType, typename StreamTraits> + concept output_streamable = requires(SubjectType subject) { + { + std::declval<std::basic_ostream<CharType, StreamTraits> &>() << subject + } -> std::same_as<std::basic_ostream<CharType, StreamTraits> &>; + }; + + template<typename SubjectType, typename CharType, typename StreamTraits> + concept nothrow_output_streamable = requires(SubjectType subject) { + requires output_streamable<SubjectType, CharType, StreamTraits>; + { + std::declval<std::basic_ostream<CharType, StreamTraits> &>() << subject + } noexcept; + }; + template<typename SubjectType> concept hashable = requires(SubjectType subject) { { diff --git a/source/lib/include/newtype/impl/type_traits_extensions.hpp b/source/lib/include/newtype/impl/type_traits_extensions.hpp index 7db8e66..22834f2 100644 --- a/source/lib/include/newtype/impl/type_traits_extensions.hpp +++ b/source/lib/include/newtype/impl/type_traits_extensions.hpp @@ -124,39 +124,6 @@ namespace nt::impl auto constexpr is_nothrow_greater_than_equal_to_comparable_v = is_nothrow_greater_than_equal_to_comparable<T>::value; } // namespace relationally_comparable - inline namespace iostreamable - { - - template<typename StreamType, typename T, typename = void> - struct is_output_streamable : std::false_type - { - }; - - template<typename StreamType, typename T> - struct is_output_streamable<StreamType, T, std::void_t<decltype(std::declval<StreamType &>() << std::declval<T const &>())>> - : std::true_type - { - }; - - template<typename StreamType, typename T> - auto constexpr is_output_streamable_v = is_output_streamable<StreamType, T>::value; - - template<typename StreamType, typename T, typename = void> - struct is_nothrow_output_streamable : std::false_type - { - }; - - template<typename StreamType, typename T> - struct is_nothrow_output_streamable<StreamType, T, std::void_t<decltype(std::declval<StreamType &>() << std::declval<T const &>())>> - : std::bool_constant<noexcept(std::declval<StreamType &>() << std::declval<T const &>())> - { - }; - - template<typename StreamType, typename T> - auto constexpr is_nothrow_output_streamable_v = is_nothrow_output_streamable<StreamType, T>::value; - - } // namespace iostreamable - inline namespace arithmetic { diff --git a/source/lib/include/newtype/newtype.hpp b/source/lib/include/newtype/newtype.hpp index acfd2f9..e40097d 100644 --- a/source/lib/include/newtype/newtype.hpp +++ b/source/lib/include/newtype/newtype.hpp @@ -335,11 +335,13 @@ namespace nt return lhs.decay() >= rhs.decay(); } - template<typename BaseType, typename TagType, auto DerivationClause, typename CharType, typename StreamTraits> + template<typename CharType, + typename StreamTraits, + nt::concepts::output_streamable<CharType, StreamTraits> BaseType, + typename TagType, + nt::contains<nt::Show> auto DerivationClause> auto operator<<(std::basic_ostream<CharType, StreamTraits> & output, new_type<BaseType, TagType, DerivationClause> const & source) noexcept( - impl::is_nothrow_output_streamable_v<std::basic_ostream<CharType, StreamTraits>, BaseType>) - -> std::enable_if_t<DerivationClause(nt::Show) && impl::is_output_streamable_v<std::basic_ostream<CharType, StreamTraits>, BaseType>, - std::basic_ostream<CharType, StreamTraits>> & + nt::concepts::nothrow_output_streamable<BaseType, CharType, StreamTraits>) -> std::basic_ostream<CharType, StreamTraits> & { return output << source.decay(); } diff --git a/source/tests/src/io_operators.cpp b/source/tests/src/io_operators.cpp index 23be171..2be41eb 100644 --- a/source/tests/src/io_operators.cpp +++ b/source/tests/src/io_operators.cpp @@ -83,7 +83,7 @@ SCENARIO("Stream Output") using type_alias = nt::new_type<int, struct tag, deriving(nt::Show)>; static_assert(has_stream_output_v<type_alias::base_type>); - THEN("it has the stream input operator") + THEN("it has the stream output operator") { STATIC_REQUIRE(has_stream_output_v<type_alias>); } |
