underlying_type.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // underlying_type.hpp ---------------------------------------------------------//
  2. // Copyright Beman Dawes, 2009
  3. // Copyright (C) 2011-2012 Vicente J. Botet Escriba
  4. // Copyright (C) 2012 Anthony Williams
  5. // Copyright (C) 2014 Andrey Semashev
  6. // Distributed under the Boost Software License, Version 1.0.
  7. // See http://www.boost.org/LICENSE_1_0.txt
  8. #ifndef BOOST_CORE_UNDERLYING_TYPE_HPP
  9. #define BOOST_CORE_UNDERLYING_TYPE_HPP
  10. #include <boost/config.hpp>
  11. // GCC 4.7 and later seem to provide std::underlying_type
  12. #if !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS) || (defined(BOOST_GCC) && BOOST_GCC >= 40700 && defined(__GXX_EXPERIMENTAL_CXX0X__))
  13. #include <type_traits>
  14. #define BOOST_DETAIL_HAS_STD_UNDERLYING_TYPE
  15. #endif
  16. #ifdef BOOST_HAS_PRAGMA_ONCE
  17. #pragma once
  18. #endif
  19. namespace boost {
  20. namespace detail {
  21. template< typename EnumType, typename Void = void >
  22. struct underlying_type_impl;
  23. #if defined(BOOST_NO_CXX11_SCOPED_ENUMS)
  24. // Support for boost/core/scoped_enum.hpp
  25. template< typename EnumType >
  26. struct underlying_type_impl< EnumType, typename EnumType::is_boost_scoped_enum_tag >
  27. {
  28. /**
  29. * The member typedef type names the underlying type of EnumType. It is EnumType::underlying_type when the EnumType is an emulated scoped enum,
  30. */
  31. typedef typename EnumType::underlying_type type;
  32. };
  33. #endif
  34. #if defined(BOOST_DETAIL_HAS_STD_UNDERLYING_TYPE)
  35. template< typename EnumType, typename Void >
  36. struct underlying_type_impl
  37. {
  38. typedef typename std::underlying_type< EnumType >::type type;
  39. };
  40. #endif
  41. } // namespace detail
  42. #if !defined(BOOST_NO_CXX11_SCOPED_ENUMS) && !defined(BOOST_DETAIL_HAS_STD_UNDERLYING_TYPE)
  43. #define BOOST_NO_UNDERLYING_TYPE
  44. #endif
  45. /**
  46. * Meta-function to get the underlying type of a scoped enum.
  47. *
  48. * Requires EnumType must be an enum type or the emulation of a scoped enum.
  49. * If BOOST_NO_UNDERLYING_TYPE is defined, the implementation will not be able
  50. * to deduce the underlying type of enums. The user is expected to specialize
  51. * this trait in this case.
  52. */
  53. template< typename EnumType >
  54. struct underlying_type :
  55. public detail::underlying_type_impl< EnumType >
  56. {
  57. };
  58. } // namespace boost
  59. #endif // BOOST_CORE_UNDERLYING_TYPE_HPP