integral_constant.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // (C) Copyright John Maddock 2015.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_TYPE_TRAITS_INTEGRAL_CONSTANT_HPP
  6. #define BOOST_TYPE_TRAITS_INTEGRAL_CONSTANT_HPP
  7. #include <boost/config.hpp>
  8. #include <boost/detail/workaround.hpp>
  9. #if (BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400)) \
  10. || BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x610)) \
  11. || BOOST_WORKAROUND(__DMC__, BOOST_TESTED_AT(0x840)) \
  12. || BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3202)) \
  13. || BOOST_WORKAROUND(BOOST_INTEL_CXX_VERSION, BOOST_TESTED_AT(810)) )\
  14. || defined(BOOST_MPL_CFG_NO_ADL_BARRIER_NAMESPACE)
  15. namespace boost{
  16. namespace mpl
  17. {
  18. template <bool B> struct bool_;
  19. template <class I, I val> struct integral_c;
  20. struct integral_c_tag;
  21. }
  22. }
  23. #else
  24. namespace mpl_{
  25. template <bool B> struct bool_;
  26. template <class I, I val> struct integral_c;
  27. struct integral_c_tag;
  28. }
  29. namespace boost
  30. {
  31. namespace mpl
  32. {
  33. using ::mpl_::bool_;
  34. using ::mpl_::integral_c;
  35. using ::mpl_::integral_c_tag;
  36. }
  37. }
  38. #endif
  39. namespace boost{
  40. template <class T, T val>
  41. struct integral_constant
  42. {
  43. typedef mpl::integral_c_tag tag;
  44. typedef T value_type;
  45. typedef integral_constant<T, val> type;
  46. static const T value = val;
  47. operator const mpl::integral_c<T, val>& ()const
  48. {
  49. static const char data[sizeof(long)] = { 0 };
  50. static const void* pdata = data;
  51. return *(reinterpret_cast<const mpl::integral_c<T, val>*>(pdata));
  52. }
  53. BOOST_CONSTEXPR operator T()const { return val; }
  54. };
  55. template <class T, T val>
  56. T const integral_constant<T, val>::value;
  57. template <bool val>
  58. struct integral_constant<bool, val>
  59. {
  60. typedef mpl::integral_c_tag tag;
  61. typedef bool value_type;
  62. typedef integral_constant<bool, val> type;
  63. static const bool value = val;
  64. operator const mpl::bool_<val>& ()const
  65. {
  66. static const char data[sizeof(long)] = { 0 };
  67. static const void* pdata = data;
  68. return *(reinterpret_cast<const mpl::bool_<val>*>(pdata));
  69. }
  70. BOOST_CONSTEXPR operator bool()const { return val; }
  71. };
  72. template <bool val>
  73. bool const integral_constant<bool, val>::value;
  74. typedef integral_constant<bool, true> true_type;
  75. typedef integral_constant<bool, false> false_type;
  76. }
  77. #endif