explicit_operator_bool.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file explicit_operator_bool.hpp
  9. * \author Andrey Semashev
  10. * \date 08.03.2009
  11. *
  12. * This header defines a compatibility macro that implements an unspecified
  13. * \c bool operator idiom, which is superseded with explicit conversion operators in
  14. * C++11.
  15. */
  16. #ifndef BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP
  17. #define BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP
  18. #include <boost/config.hpp>
  19. #include <boost/config/workaround.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  24. /*!
  25. * \brief The macro defines an explicit operator of conversion to \c bool
  26. *
  27. * The macro should be used inside the definition of a class that has to
  28. * support the conversion. The class should also implement <tt>operator!</tt>,
  29. * in terms of which the conversion operator will be implemented.
  30. */
  31. #define BOOST_EXPLICIT_OPERATOR_BOOL()\
  32. BOOST_FORCEINLINE explicit operator bool () const\
  33. {\
  34. return !this->operator! ();\
  35. }
  36. /*!
  37. * \brief The macro defines a noexcept explicit operator of conversion to \c bool
  38. *
  39. * The macro should be used inside the definition of a class that has to
  40. * support the conversion. The class should also implement <tt>operator!</tt>,
  41. * in terms of which the conversion operator will be implemented.
  42. */
  43. #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\
  44. BOOST_FORCEINLINE explicit operator bool () const BOOST_NOEXCEPT\
  45. {\
  46. return !this->operator! ();\
  47. }
  48. #if !BOOST_WORKAROUND(BOOST_GCC, < 40700)
  49. /*!
  50. * \brief The macro defines a constexpr explicit operator of conversion to \c bool
  51. *
  52. * The macro should be used inside the definition of a class that has to
  53. * support the conversion. The class should also implement <tt>operator!</tt>,
  54. * in terms of which the conversion operator will be implemented.
  55. */
  56. #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
  57. BOOST_FORCEINLINE BOOST_CONSTEXPR explicit operator bool () const BOOST_NOEXCEPT\
  58. {\
  59. return !this->operator! ();\
  60. }
  61. #else
  62. #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL() BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
  63. #endif
  64. #else // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  65. #if (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG)
  66. // Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it
  67. #define BOOST_NO_UNSPECIFIED_BOOL
  68. #endif // (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG)
  69. #if !defined(BOOST_NO_UNSPECIFIED_BOOL)
  70. namespace boost {
  71. namespace detail {
  72. #if !defined(_MSC_VER) && !defined(__IBMCPP__)
  73. struct unspecified_bool
  74. {
  75. // NOTE TO THE USER: If you see this in error messages then you tried
  76. // to apply an unsupported operator on the object that supports
  77. // explicit conversion to bool.
  78. struct OPERATORS_NOT_ALLOWED;
  79. static void true_value(OPERATORS_NOT_ALLOWED*) {}
  80. };
  81. typedef void (*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*);
  82. #else
  83. // MSVC and VACPP are too eager to convert pointer to function to void* even though they shouldn't
  84. struct unspecified_bool
  85. {
  86. // NOTE TO THE USER: If you see this in error messages then you tried
  87. // to apply an unsupported operator on the object that supports
  88. // explicit conversion to bool.
  89. struct OPERATORS_NOT_ALLOWED;
  90. void true_value(OPERATORS_NOT_ALLOWED*) {}
  91. };
  92. typedef void (unspecified_bool::*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*);
  93. #endif
  94. } // namespace detail
  95. } // namespace boost
  96. #define BOOST_EXPLICIT_OPERATOR_BOOL()\
  97. BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const\
  98. {\
  99. return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
  100. }
  101. #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\
  102. BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\
  103. {\
  104. return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
  105. }
  106. #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
  107. BOOST_FORCEINLINE BOOST_CONSTEXPR operator boost::detail::unspecified_bool_type () const BOOST_NOEXCEPT\
  108. {\
  109. return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
  110. }
  111. #else // !defined(BOOST_NO_UNSPECIFIED_BOOL)
  112. #define BOOST_EXPLICIT_OPERATOR_BOOL()\
  113. BOOST_FORCEINLINE operator bool () const\
  114. {\
  115. return !this->operator! ();\
  116. }
  117. #define BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()\
  118. BOOST_FORCEINLINE operator bool () const BOOST_NOEXCEPT\
  119. {\
  120. return !this->operator! ();\
  121. }
  122. #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
  123. BOOST_FORCEINLINE BOOST_CONSTEXPR operator bool () const BOOST_NOEXCEPT\
  124. {\
  125. return !this->operator! ();\
  126. }
  127. #endif // !defined(BOOST_NO_UNSPECIFIED_BOOL)
  128. #endif // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  129. #endif // BOOST_CORE_EXPLICIT_OPERATOR_BOOL_HPP