Add detail/is_scoped_enum.hpp

This commit is contained in:
Peter Dimov
2020-05-02 20:23:35 +03:00
parent 21eed3fd59
commit ceb58197d5
3 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,35 @@
#ifndef BOOST_ENDIAN_DETAIL_IS_SCOPED_ENUM_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_IS_SCOPED_ENUM_HPP_INCLUDED
// Copyright 2020 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/type_traits/conditional.hpp>
#include <boost/type_traits/is_enum.hpp>
#include <boost/type_traits/is_convertible.hpp>
namespace boost
{
namespace endian
{
namespace detail
{
template<class T> struct negation: boost::integral_constant<bool, !T::value> {};
template<class T> struct is_scoped_enum:
boost::conditional<
boost::is_enum<T>::value,
negation<boost::is_convertible<T, int>>,
boost::false_type
>::type
{
};
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_IS_SCOPED_ENUM_HPP_INCLUDED

View File

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

View File

@ -0,0 +1,68 @@
// Copyright 2020 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/endian/detail/is_scoped_enum.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/config.hpp>
enum E1 {};
#if !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
enum E2: long {};
enum class E3 {};
enum class E4: long {};
#endif
struct X
{
operator int() const { return 0; }
};
struct Y;
template<class T> void test_true()
{
using boost::endian::detail::is_scoped_enum;
BOOST_TEST_TRAIT_TRUE((is_scoped_enum<T>));
BOOST_TEST_TRAIT_TRUE((is_scoped_enum<T const>));
BOOST_TEST_TRAIT_TRUE((is_scoped_enum<T volatile>));
BOOST_TEST_TRAIT_TRUE((is_scoped_enum<T const volatile>));
}
template<class T> void test_false()
{
using boost::endian::detail::is_scoped_enum;
BOOST_TEST_TRAIT_FALSE((is_scoped_enum<T>));
BOOST_TEST_TRAIT_FALSE((is_scoped_enum<T const>));
BOOST_TEST_TRAIT_FALSE((is_scoped_enum<T volatile>));
BOOST_TEST_TRAIT_FALSE((is_scoped_enum<T const volatile>));
}
int main()
{
test_false<int>();
test_false<bool>();
test_false<X>();
test_false<Y>();
test_false<void>();
test_false<int[]>();
test_false<int[1]>();
test_false<E1>();
#if !defined(BOOST_NO_CXX11_SCOPED_ENUMS)
test_false<E2>();
test_true<E3>();
test_true<E4>();
#endif
return boost::report_errors();
}