From d6e0893a4b2d1448c95021ada86a3562b5438ee8 Mon Sep 17 00:00:00 2001 From: Wim Haeck Date: Mon, 6 Nov 2023 17:30:25 -0700 Subject: [PATCH] Replacing sfinae with static_assert --- src/tools/ranges/ViewBase.hpp | 128 ++++++++-------------------------- 1 file changed, 28 insertions(+), 100 deletions(-) diff --git a/src/tools/ranges/ViewBase.hpp b/src/tools/ranges/ViewBase.hpp index f9ea6dd..c07ea11 100644 --- a/src/tools/ranges/ViewBase.hpp +++ b/src/tools/ranges/ViewBase.hpp @@ -40,55 +40,60 @@ class ViewBase { /** * @brief Return whether or not the view is empty */ - template < typename Range = Derived, - concepts::Requires< true, concepts::IsForwardIterator, - typename Range::iterator > = true > constexpr bool empty() const noexcept { + static_assert( + concepts::IsForwardIterator< typename Derived::iterator >::value == true, + "the empty() method can only be made available for forward iterators" ); + return this->derived().begin() == this->derived().end(); } /** * @brief Return whether or not the view is empty */ - template < typename Range = Derived, - concepts::Requires< true, concepts::IsForwardIterator, - typename Range::iterator > = true > constexpr explicit operator bool() const noexcept { + static_assert( + concepts::IsForwardIterator< typename Derived::iterator >::value == true, + "the operator bool() method can only be made available for forward iterators" ); + return this->empty(); } /** * @brief Return the size of the iterator view */ - template < typename Range = Derived, - concepts::Requires< true, concepts::IsForwardIterator, - typename Range::iterator > = true > constexpr std::size_t size() const noexcept { + static_assert( + concepts::IsForwardIterator< typename Derived::iterator >::value == true, + "the size() method can only be made available for forward iterators" ); + return std::distance( this->derived().begin(), this->derived().end() ); } /** * @brief Return the reference to the front element of the view */ - template < typename Range = Derived, - concepts::Requires< true, concepts::IsForwardIterator, - typename Range::iterator > = true > constexpr decltype(auto) front() const noexcept { + static_assert( + concepts::IsForwardIterator< typename Derived::iterator >::value == true, + "the front() method can only be made available for forward iterators" ); + return *( this->derived().begin() ); } /** * @brief Return the reference to the back element of the view */ - template < typename Range = Derived, - concepts::Requires< true, concepts::IsBidirectionalIterator, - typename Range::iterator > = true > constexpr decltype(auto) back() const noexcept { + static_assert( + concepts::IsBidirectionalIterator< typename Derived::iterator >::value == true, + "the back() method can only be made available for bidirectional iterators" ); + return *( std::prev( this->derived().end() ) ); } @@ -99,11 +104,12 @@ class ViewBase { * * @param[in] i the index */ - template < typename Range = Derived, - concepts::Requires< true, concepts::IsRandomAccessIterator, - typename Range::iterator > = true > constexpr decltype(auto) operator[]( std::size_t i ) const noexcept { + static_assert( + concepts::IsRandomAccessIterator< typename Derived::iterator >::value == true, + "the operator[] method can only be made available for random access iterators" ); + return *( std::next( this->derived().begin(), i ) ); } @@ -112,11 +118,12 @@ class ViewBase { * * @param[in] i the index */ - template < typename Range = Derived, - concepts::Requires< true, concepts::IsRandomAccessIterator, - typename Range::iterator > = true > constexpr decltype(auto) at( std::size_t i ) const { + static_assert( + concepts::IsRandomAccessIterator< typename Derived::iterator >::value == true, + "the at() method can only be made available for random access iterators" ); + if ( i >= this->size() ) { throw std::out_of_range( "Index out of range: " + @@ -154,85 +161,6 @@ class ViewBase { return !this->operator==( other ); } - - // the following code is added to make compiler errors more comprehensible - - template < typename Range = Derived, - concepts::Requires< false, concepts::IsForwardIterator, - typename Range::iterator > = true > - constexpr bool empty() const noexcept { - - static_assert( - concepts::IsForwardIterator< typename Range::iterator >::value == true, - "the empty() method can only be made available for forward iterators" ); - return true; - } - - template < typename Range = Derived, - concepts::Requires< false, concepts::IsForwardIterator, - typename Range::iterator > = true > - constexpr explicit operator bool() const noexcept { - - static_assert( - concepts::IsForwardIterator< typename Range::iterator >::value == true, - "the operator bool() method can only be made available for forward iterators" ); - return true; - } - - template < typename Range = Derived, - concepts::Requires< false, concepts::IsForwardIterator, - typename Range::iterator > = true > - constexpr std::size_t size() const noexcept { - - static_assert( - concepts::IsForwardIterator< typename Range::iterator >::value == true, - "the size() method can only be made available for forward iterators" ); - return std::size_t{}; - } - - template < typename Range = Derived, - concepts::Requires< false, concepts::IsForwardIterator, - typename Range::iterator > = true > - constexpr decltype(auto) front() const noexcept { - - static_assert( - concepts::IsBidirectionalIterator< typename Range::iterator >::value == true, - "the front() method can only be made available for bidirectional iterators" ); - return typename Derived::value_type{}; - } - - template < typename Range = Derived, - concepts::Requires< false, concepts::IsBidirectionalIterator, - typename Range::iterator > = true > - constexpr decltype(auto) back() const noexcept { - - static_assert( - concepts::IsBidirectionalIterator< typename Range::iterator >::value == true, - "the back() method can only be made available for bidirectional iterators" ); - return typename Derived::value_type{}; - } - - template < typename Range = Derived, - concepts::Requires< false, concepts::IsRandomAccessIterator, - typename Range::iterator > = true > - constexpr decltype(auto) operator[]( std::size_t i ) const { - - static_assert( - concepts::IsRandomAccessIterator< typename Range::iterator >::value == true, - "the operator[] method can only be made available for random access iterators" ); - return typename Derived::value_type{}; - } - - template < typename Range = Derived, - concepts::Requires< false, concepts::IsRandomAccessIterator, - typename Range::iterator > = true > - constexpr decltype(auto) at( std::size_t i ) const { - - static_assert( - concepts::IsRandomAccessIterator< typename Range::iterator >::value == true, - "the at() method can only be made available for random access iterators" ); - return typename Derived::value_type{}; - } }; } // ranges namespace