is_copy_assignable.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // (C) Copyright Ion Gaztanaga 2014.
  2. //
  3. // Use, modification and distribution are subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt).
  6. //
  7. // See http://www.boost.org/libs/type_traits for most recent version including documentation.
  8. #ifndef BOOST_TT_IS_COPY_ASSIGNABLE_HPP_INCLUDED
  9. #define BOOST_TT_IS_COPY_ASSIGNABLE_HPP_INCLUDED
  10. #include <boost/config.hpp>
  11. #include <boost/type_traits/detail/yes_no_type.hpp>
  12. #include <boost/type_traits/is_noncopyable.hpp>
  13. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) && !defined(BOOST_NO_CXX11_DECLTYPE) \
  14. && !defined(BOOST_INTEL_CXX_VERSION) && \
  15. !(defined(BOOST_MSVC) && _MSC_VER == 1800)
  16. #define BOOST_TT_CXX11_IS_COPY_ASSIGNABLE
  17. #include <boost/type_traits/declval.hpp>
  18. #else
  19. //For compilers without decltype
  20. #include <boost/type_traits/is_const.hpp>
  21. #include <boost/type_traits/is_array.hpp>
  22. #include <boost/type_traits/add_reference.hpp>
  23. #include <boost/type_traits/remove_reference.hpp>
  24. #endif
  25. namespace boost {
  26. namespace detail{
  27. template <bool DerivedFromNoncopyable, class T>
  28. struct is_copy_assignable_impl2 {
  29. // Intel compiler has problems with SFINAE for copy constructors and deleted functions:
  30. //
  31. // error: function *function_name* cannot be referenced -- it is a deleted function
  32. // static boost::type_traits::yes_type test(T1&, decltype(T1(boost::declval<T1&>()))* = 0);
  33. // ^
  34. //
  35. // MSVC 12.0 (Visual 2013) has problems when the copy constructor has been deleted. See:
  36. // https://connect.microsoft.com/VisualStudio/feedback/details/800328/std-is-copy-constructible-is-broken
  37. #if defined(BOOST_TT_CXX11_IS_COPY_ASSIGNABLE)
  38. typedef boost::type_traits::yes_type yes_type;
  39. typedef boost::type_traits::no_type no_type;
  40. template <class U>
  41. static decltype(::boost::declval<U&>() = ::boost::declval<const U&>(), yes_type() ) test(int);
  42. template <class>
  43. static no_type test(...);
  44. static const bool value = sizeof(test<T>(0)) == sizeof(yes_type);
  45. #else
  46. static BOOST_DEDUCED_TYPENAME boost::add_reference<T>::type produce();
  47. template <class T1>
  48. static boost::type_traits::no_type test(T1&, typename T1::boost_move_no_copy_constructor_or_assign* = 0);
  49. static boost::type_traits::yes_type test(...);
  50. // If you see errors like this:
  51. //
  52. // `'T::operator=(const T&)' is private`
  53. // `boost/type_traits/is_copy_assignable.hpp:NN:M: error: within this context`
  54. //
  55. // then you are trying to call that macro for a structure defined like that:
  56. //
  57. // struct T {
  58. // ...
  59. // private:
  60. // T & operator=(const T &);
  61. // ...
  62. // };
  63. //
  64. // To fix that you must modify your structure:
  65. //
  66. // // C++03 and C++11 version
  67. // struct T: private boost::noncopyable {
  68. // ...
  69. // private:
  70. // T & operator=(const T &);
  71. // ...
  72. // };
  73. //
  74. // // C++11 version
  75. // struct T {
  76. // ...
  77. // private:
  78. // T& operator=(const T &) = delete;
  79. // ...
  80. // };
  81. BOOST_STATIC_CONSTANT(bool, value = (
  82. sizeof(test(produce())) == sizeof(boost::type_traits::yes_type)
  83. ));
  84. #endif
  85. };
  86. template <class T>
  87. struct is_copy_assignable_impl2<true, T> {
  88. BOOST_STATIC_CONSTANT(bool, value = false);
  89. };
  90. template <class T>
  91. struct is_copy_assignable_impl {
  92. #if !defined(BOOST_TT_CXX11_IS_COPY_ASSIGNABLE)
  93. //For compilers without decltype, at least return false on const types, arrays
  94. //types derived from boost::noncopyable and types defined as BOOST_MOVEABLE_BUT_NOT_COPYABLE
  95. typedef BOOST_DEDUCED_TYPENAME boost::remove_reference<T>::type unreferenced_t;
  96. BOOST_STATIC_CONSTANT(bool, value = (
  97. boost::detail::is_copy_assignable_impl2<
  98. boost::is_noncopyable<T>::value
  99. || boost::is_const<unreferenced_t>::value || boost::is_array<unreferenced_t>::value
  100. ,T
  101. >::value
  102. ));
  103. #else
  104. BOOST_STATIC_CONSTANT(bool, value = (
  105. boost::detail::is_copy_assignable_impl2<
  106. boost::is_noncopyable<T>::value,T
  107. >::value
  108. ));
  109. #endif
  110. };
  111. } // namespace detail
  112. template <class T> struct is_copy_assignable : public integral_constant<bool, ::boost::detail::is_copy_assignable_impl<T>::value>{};
  113. template <> struct is_copy_assignable<void> : public false_type{};
  114. #ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS
  115. template <> struct is_copy_assignable<void const> : public false_type{};
  116. template <> struct is_copy_assignable<void const volatile> : public false_type{};
  117. template <> struct is_copy_assignable<void volatile> : public false_type{};
  118. #endif
  119. } // namespace boost
  120. #endif // BOOST_TT_IS_COPY_ASSIGNABLE_HPP_INCLUDED