explicit_operator_bool.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_UTILITY_EXPLICIT_OPERATOR_BOOL_HPP_INCLUDED_
  17. #define BOOST_UTILITY_EXPLICIT_OPERATOR_BOOL_HPP_INCLUDED_
  18. #include <boost/config.hpp>
  19. #ifdef BOOST_HAS_PRAGMA_ONCE
  20. #pragma once
  21. #endif
  22. #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  23. /*!
  24. * \brief The macro defines an explicit operator of conversion to \c bool
  25. *
  26. * The macro should be used inside the definition of a class that has to
  27. * support the conversion. The class should also implement <tt>operator!</tt>,
  28. * in terms of which the conversion operator will be implemented.
  29. */
  30. #define BOOST_EXPLICIT_OPERATOR_BOOL()\
  31. BOOST_FORCEINLINE explicit operator bool () const\
  32. {\
  33. return !this->operator! ();\
  34. }
  35. /*!
  36. * \brief The macro defines a constexpr explicit operator of conversion to \c bool
  37. *
  38. * The macro should be used inside the definition of a class that has to
  39. * support the conversion. The class should also implement <tt>operator!</tt>,
  40. * in terms of which the conversion operator will be implemented.
  41. */
  42. #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
  43. BOOST_FORCEINLINE BOOST_CONSTEXPR explicit operator bool () const\
  44. {\
  45. return !this->operator! ();\
  46. }
  47. #else // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  48. #if (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG)
  49. // Sun C++ 5.3 can't handle the safe_bool idiom, so don't use it
  50. #define BOOST_NO_UNSPECIFIED_BOOL
  51. #endif // (defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)) && !defined(BOOST_NO_COMPILER_CONFIG)
  52. #if !defined(BOOST_NO_UNSPECIFIED_BOOL)
  53. namespace boost {
  54. namespace detail {
  55. #if !defined(_MSC_VER) && !defined(__IBMCPP__)
  56. struct unspecified_bool
  57. {
  58. // NOTE TO THE USER: If you see this in error messages then you tried
  59. // to apply an unsupported operator on the object that supports
  60. // explicit conversion to bool.
  61. struct OPERATORS_NOT_ALLOWED;
  62. static void true_value(OPERATORS_NOT_ALLOWED*) {}
  63. };
  64. typedef void (*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*);
  65. #else
  66. // MSVC and VACPP are too eager to convert pointer to function to void* even though they shouldn't
  67. struct unspecified_bool
  68. {
  69. // NOTE TO THE USER: If you see this in error messages then you tried
  70. // to apply an unsupported operator on the object that supports
  71. // explicit conversion to bool.
  72. struct OPERATORS_NOT_ALLOWED;
  73. void true_value(OPERATORS_NOT_ALLOWED*) {}
  74. };
  75. typedef void (unspecified_bool::*unspecified_bool_type)(unspecified_bool::OPERATORS_NOT_ALLOWED*);
  76. #endif
  77. } // namespace detail
  78. } // namespace boost
  79. #define BOOST_EXPLICIT_OPERATOR_BOOL()\
  80. BOOST_FORCEINLINE operator boost::detail::unspecified_bool_type () const\
  81. {\
  82. return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
  83. }
  84. #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
  85. BOOST_FORCEINLINE BOOST_CONSTEXPR operator boost::detail::unspecified_bool_type () const\
  86. {\
  87. return (!this->operator! () ? &boost::detail::unspecified_bool::true_value : (boost::detail::unspecified_bool_type)0);\
  88. }
  89. #else // !defined(BOOST_NO_UNSPECIFIED_BOOL)
  90. #define BOOST_EXPLICIT_OPERATOR_BOOL()\
  91. BOOST_FORCEINLINE operator bool () const\
  92. {\
  93. return !this->operator! ();\
  94. }
  95. #define BOOST_CONSTEXPR_EXPLICIT_OPERATOR_BOOL()\
  96. BOOST_FORCEINLINE BOOST_CONSTEXPR operator bool () const\
  97. {\
  98. return !this->operator! ();\
  99. }
  100. #endif // !defined(BOOST_NO_UNSPECIFIED_BOOL)
  101. #endif // !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
  102. #endif // BOOST_UTILITY_EXPLICIT_OPERATOR_BOOL_HPP_INCLUDED_