mirror of
https://github.com/boostorg/endian.git
synced 2025-07-31 13:07:24 +02:00
Constrain the primary endian_reverse to not apply to class types; add test. Fixes #41.
This commit is contained in:
@@ -46,7 +46,8 @@ namespace endian
|
|||||||
// reverse byte order
|
// reverse byte order
|
||||||
// requires T to be a non-bool integral type
|
// requires T to be a non-bool integral type
|
||||||
// in detail/endian_reverse.hpp
|
// 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
|
// reverse byte order unless native endianness is big
|
||||||
template <class EndianReversible >
|
template <class EndianReversible >
|
||||||
@@ -109,8 +110,10 @@ namespace endian
|
|||||||
|
|
||||||
// reverse in place
|
// reverse in place
|
||||||
// in detail/endian_reverse.hpp
|
// in detail/endian_reverse.hpp
|
||||||
template <class EndianReversible>
|
//
|
||||||
inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT;
|
// template <class EndianReversible>
|
||||||
|
// inline void endian_reverse_inplace(EndianReversible& x) BOOST_NOEXCEPT;
|
||||||
|
//
|
||||||
// Effects: x = endian_reverse(x)
|
// Effects: x = endian_reverse(x)
|
||||||
|
|
||||||
// reverse in place unless native endianness is big
|
// reverse in place unless native endianness is big
|
||||||
|
@@ -10,6 +10,8 @@
|
|||||||
#include <boost/endian/detail/intrinsic.hpp>
|
#include <boost/endian/detail/intrinsic.hpp>
|
||||||
#include <boost/type_traits/is_integral.hpp>
|
#include <boost/type_traits/is_integral.hpp>
|
||||||
#include <boost/type_traits/is_same.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/static_assert.hpp>
|
||||||
#include <boost/cstdint.hpp>
|
#include <boost/cstdint.hpp>
|
||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
@@ -106,7 +108,9 @@ inline uint128_type BOOST_ENDIAN_CONSTEXPR endian_reverse_impl( uint128_type x )
|
|||||||
// Requires:
|
// Requires:
|
||||||
// T is non-bool integral
|
// 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) );
|
BOOST_STATIC_ASSERT( is_integral<T>::value && !(is_same<T, bool>::value) );
|
||||||
|
|
||||||
|
@@ -94,3 +94,5 @@ run endian_hpp_test.cpp ;
|
|||||||
run-ni endian_hpp_test.cpp ;
|
run-ni endian_hpp_test.cpp ;
|
||||||
|
|
||||||
run order_test.cpp ;
|
run order_test.cpp ;
|
||||||
|
|
||||||
|
run endian_reverse_test2.cpp ;
|
||||||
|
39
test/endian_reverse_test2.cpp
Normal file
39
test/endian_reverse_test2.cpp
Normal 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();
|
||||||
|
}
|
Reference in New Issue
Block a user