Add detail/is_fixed_enum.hpp

This commit is contained in:
Peter Dimov
2020-05-02 21:16:12 +03:00
parent 7f025ef4b6
commit e2bb07e160
3 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,51 @@
#ifndef BOOST_ENDIAN_DETAIL_IS_FIXED_ENUM_HPP_INCLUDED
#define BOOST_ENDIAN_DETAIL_IS_FIXED_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/endian/detail/is_scoped_enum.hpp>
#include <boost/type_traits/is_enum.hpp>
#include <boost/type_traits/remove_cv.hpp>
#include <boost/config.hpp>
namespace boost
{
namespace endian
{
namespace detail
{
#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
// Thanks to Will Wray for the T{0} idea, which unfortunately only works in C++17+
//
// VS2017 works, VS2019 works from 19.20 to 19.22, then in 19.23 starts accepting
// T{0} for unscoped non-fixed enums as well. Sad!
template<class T, class E = T> struct is_fixed_enum_impl: boost::false_type
{
};
template<class T> struct is_fixed_enum_impl<T, decltype(T{0})>: boost::is_enum<T>
{
};
template<class T> struct is_fixed_enum: is_fixed_enum_impl< boost::remove_cv_t<T> >
{
};
#else
template<class T> struct is_fixed_enum: is_scoped_enum<T>
{
};
#endif
} // namespace detail
} // namespace endian
} // namespace boost
#endif // BOOST_ENDIAN_DETAIL_IS_FIXED_ENUM_HPP_INCLUDED

View File

@ -98,3 +98,4 @@ run order_test.cpp ;
run endian_reverse_test2.cpp ;
run is_scoped_enum_test.cpp ;
run is_fixed_enum_test.cpp ;

View File

@ -0,0 +1,72 @@
// 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_fixed_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_fixed_enum;
BOOST_TEST_TRAIT_TRUE((is_fixed_enum<T>));
BOOST_TEST_TRAIT_TRUE((is_fixed_enum<T const>));
BOOST_TEST_TRAIT_TRUE((is_fixed_enum<T volatile>));
BOOST_TEST_TRAIT_TRUE((is_fixed_enum<T const volatile>));
}
template<class T> void test_false()
{
using boost::endian::detail::is_fixed_enum;
BOOST_TEST_TRAIT_FALSE((is_fixed_enum<T>));
BOOST_TEST_TRAIT_FALSE((is_fixed_enum<T const>));
BOOST_TEST_TRAIT_FALSE((is_fixed_enum<T volatile>));
BOOST_TEST_TRAIT_FALSE((is_fixed_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)
#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
test_true<E2>();
#endif
test_true<E3>();
test_true<E4>();
#endif
return boost::report_errors();
}