Constrain the primary endian_reverse to not apply to class types; add test. Fixes #41.

This commit is contained in:
Peter Dimov
2020-01-13 16:22:16 +02:00
parent 3d053160ce
commit 69c020ea8f
4 changed files with 53 additions and 5 deletions

View File

@ -46,7 +46,8 @@ namespace endian
// reverse byte order
// requires T to be a non-bool integral type
// in detail/endian_reverse.hpp
template<class T> inline BOOST_CONSTEXPR T endian_reverse( T x ) BOOST_NOEXCEPT;
//
// template<class T> inline BOOST_CONSTEXPR T endian_reverse( T x ) BOOST_NOEXCEPT;
// reverse byte order unless native endianness is big
template <class EndianReversible >
@ -109,9 +110,11 @@ namespace endian
// reverse in place
// in detail/endian_reverse.hpp
template <class EndianReversible>
inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT;
// Effects: x = endian_reverse(x)
//
// template <class EndianReversible>
// inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT;
//
// Effects: x = endian_reverse(x)
// reverse in place unless native endianness is big
template <class EndianReversibleInplace>

View File

@ -10,6 +10,8 @@
#include <boost/endian/detail/intrinsic.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/enable_if.hpp>
#include <boost/type_traits/is_class.hpp>
#include <boost/static_assert.hpp>
#include <boost/cstdint.hpp>
#include <boost/config.hpp>
@ -106,7 +108,9 @@ inline uint128_type BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint128_type x )
// Requires:
// T is non-bool integral
template<class T> inline BOOST_CONSTEXPR T endian_reverse( T x ) BOOST_NOEXCEPT
template<class T> inline BOOST_CONSTEXPR
typename enable_if_< !is_class<T>::value, T >::type
endian_reverse( T x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( is_integral<T>::value && !(is_same<T, bool>::value) );

View File

@ -94,3 +94,5 @@ run endian_hpp_test.cpp ;
run-ni endian_hpp_test.cpp ;
run order_test.cpp ;
run endian_reverse_test2.cpp ;

View File

@ -0,0 +1,39 @@
// Copyright 2019, 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/conversion.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/type_traits/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/cstdint.hpp>
namespace N
{
struct X
{
boost::uint32_t m;
};
template<class T> typename boost::enable_if_<boost::is_same<T, X>::value, T>::type endian_reverse( T x )
{
using boost::endian::endian_reverse;
X r = { endian_reverse( x.m ) };
return r;
}
} // namespace N
int main()
{
using namespace boost::endian;
N::X x1 = { 0x01020304 };
N::X x2 = endian_reverse( x1 );
BOOST_TEST_EQ( x2.m, 0x04030201 );
return boost::report_errors();
}