has_new_operator.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // (C) Copyright Runar Undheim, Robert Ramey & John Maddock 2008.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt).
  5. //
  6. // See http://www.boost.org/libs/type_traits for most recent version including documentation.
  7. #ifndef BOOST_TT_HAS_NEW_OPERATOR_HPP_INCLUDED
  8. #define BOOST_TT_HAS_NEW_OPERATOR_HPP_INCLUDED
  9. #include <new> // std::nothrow_t
  10. #include <cstddef> // std::size_t
  11. #include <boost/type_traits/integral_constant.hpp>
  12. #include <boost/type_traits/detail/yes_no_type.hpp>
  13. #include <boost/detail/workaround.hpp>
  14. #if defined(new)
  15. # if BOOST_WORKAROUND(BOOST_MSVC, >= 1310)
  16. # define BOOST_TT_AUX_MACRO_NEW_DEFINED
  17. # pragma push_macro("new")
  18. # undef new
  19. # else
  20. # error "Sorry but you can't include this header if 'new' is defined as a macro."
  21. # endif
  22. #endif
  23. namespace boost {
  24. namespace detail {
  25. template <class U, U x>
  26. struct test;
  27. template <typename T>
  28. struct has_new_operator_impl {
  29. template<class U>
  30. static type_traits::yes_type check_sig1(
  31. U*,
  32. test<
  33. void *(*)(std::size_t),
  34. &U::operator new
  35. >* = NULL
  36. );
  37. template<class U>
  38. static type_traits::no_type check_sig1(...);
  39. template<class U>
  40. static type_traits::yes_type check_sig2(
  41. U*,
  42. test<
  43. void *(*)(std::size_t, const std::nothrow_t&),
  44. &U::operator new
  45. >* = NULL
  46. );
  47. template<class U>
  48. static type_traits::no_type check_sig2(...);
  49. template<class U>
  50. static type_traits::yes_type check_sig3(
  51. U*,
  52. test<
  53. void *(*)(std::size_t, void*),
  54. &U::operator new
  55. >* = NULL
  56. );
  57. template<class U>
  58. static type_traits::no_type check_sig3(...);
  59. template<class U>
  60. static type_traits::yes_type check_sig4(
  61. U*,
  62. test<
  63. void *(*)(std::size_t),
  64. &U::operator new[]
  65. >* = NULL
  66. );
  67. template<class U>
  68. static type_traits::no_type check_sig4(...);
  69. template<class U>
  70. static type_traits::yes_type check_sig5(
  71. U*,
  72. test<
  73. void *(*)(std::size_t, const std::nothrow_t&),
  74. &U::operator new[]
  75. >* = NULL
  76. );
  77. template<class U>
  78. static type_traits::no_type check_sig5(...);
  79. template<class U>
  80. static type_traits::yes_type check_sig6(
  81. U*,
  82. test<
  83. void *(*)(std::size_t, void*),
  84. &U::operator new[]
  85. >* = NULL
  86. );
  87. template<class U>
  88. static type_traits::no_type check_sig6(...);
  89. // GCC2 won't even parse this template if we embed the computation
  90. // of s1 in the computation of value.
  91. #ifdef __GNUC__
  92. BOOST_STATIC_CONSTANT(unsigned, s1 = sizeof(has_new_operator_impl<T>::template check_sig1<T>(0)));
  93. BOOST_STATIC_CONSTANT(unsigned, s2 = sizeof(has_new_operator_impl<T>::template check_sig2<T>(0)));
  94. BOOST_STATIC_CONSTANT(unsigned, s3 = sizeof(has_new_operator_impl<T>::template check_sig3<T>(0)));
  95. BOOST_STATIC_CONSTANT(unsigned, s4 = sizeof(has_new_operator_impl<T>::template check_sig4<T>(0)));
  96. BOOST_STATIC_CONSTANT(unsigned, s5 = sizeof(has_new_operator_impl<T>::template check_sig5<T>(0)));
  97. BOOST_STATIC_CONSTANT(unsigned, s6 = sizeof(has_new_operator_impl<T>::template check_sig6<T>(0)));
  98. #else
  99. #if BOOST_WORKAROUND(BOOST_MSVC_FULL_VER, >= 140050000)
  100. #pragma warning(push)
  101. #pragma warning(disable:6334)
  102. #endif
  103. BOOST_STATIC_CONSTANT(unsigned, s1 = sizeof(check_sig1<T>(0)));
  104. BOOST_STATIC_CONSTANT(unsigned, s2 = sizeof(check_sig2<T>(0)));
  105. BOOST_STATIC_CONSTANT(unsigned, s3 = sizeof(check_sig3<T>(0)));
  106. BOOST_STATIC_CONSTANT(unsigned, s4 = sizeof(check_sig4<T>(0)));
  107. BOOST_STATIC_CONSTANT(unsigned, s5 = sizeof(check_sig5<T>(0)));
  108. BOOST_STATIC_CONSTANT(unsigned, s6 = sizeof(check_sig6<T>(0)));
  109. #if BOOST_WORKAROUND(BOOST_MSVC_FULL_VER, >= 140050000)
  110. #pragma warning(pop)
  111. #endif
  112. #endif
  113. BOOST_STATIC_CONSTANT(bool, value =
  114. (s1 == sizeof(type_traits::yes_type)) ||
  115. (s2 == sizeof(type_traits::yes_type)) ||
  116. (s3 == sizeof(type_traits::yes_type)) ||
  117. (s4 == sizeof(type_traits::yes_type)) ||
  118. (s5 == sizeof(type_traits::yes_type)) ||
  119. (s6 == sizeof(type_traits::yes_type))
  120. );
  121. };
  122. } // namespace detail
  123. template <class T> struct has_new_operator : public integral_constant<bool, ::boost::detail::has_new_operator_impl<T>::value>{};
  124. } // namespace boost
  125. #if defined(BOOST_TT_AUX_MACRO_NEW_DEFINED)
  126. # pragma pop_macro("new")
  127. #endif
  128. #endif // BOOST_TT_HAS_NEW_OPERATOR_HPP_INCLUDED