is_copy_constructible.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // (C) Copyright Antony Polukhin 2013.
  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_CONSTRUCTIBLE_HPP_INCLUDED
  9. #define BOOST_TT_IS_COPY_CONSTRUCTIBLE_HPP_INCLUDED
  10. #include <boost/config.hpp>
  11. #include <boost/detail/workaround.hpp>
  12. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800) && !BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40900)
  13. #include <boost/type_traits/is_constructible.hpp>
  14. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1800)
  15. namespace boost {
  16. template <class T> struct is_copy_constructible : public boost::is_constructible<T, const T&>{};
  17. template <> struct is_copy_constructible<void> : public false_type{};
  18. template <> struct is_copy_constructible<void const> : public false_type{};
  19. template <> struct is_copy_constructible<void const volatile> : public false_type{};
  20. template <> struct is_copy_constructible<void volatile> : public false_type{};
  21. } // namespace boost
  22. #else
  23. //
  24. // Special version for VC12 which has a problem when a base class (such as non_copyable) has a deleted
  25. // copy constructor. In this case the compiler thinks there really is a copy-constructor and tries to
  26. // instantiate the deleted member. std::is_copy_constructible has the same issue (or at least returns
  27. // an incorrect value, which just defers the issue into the users code) as well. We can at least fix
  28. // boost::non_copyable as a base class as a special case:
  29. //
  30. #include <boost/type_traits/is_noncopyable.hpp>
  31. namespace boost {
  32. namespace detail
  33. {
  34. template <class T, bool b> struct is_copy_constructible_imp : public boost::is_constructible<T, const T&>{};
  35. template <class T> struct is_copy_constructible_imp<T, true> : public false_type{};
  36. }
  37. template <class T> struct is_copy_constructible : public detail::is_copy_constructible_imp<T, is_noncopyable<T>::value>{};
  38. template <> struct is_copy_constructible<void> : public false_type{};
  39. template <> struct is_copy_constructible<void const> : public false_type{};
  40. template <> struct is_copy_constructible<void const volatile> : public false_type{};
  41. template <> struct is_copy_constructible<void volatile> : public false_type{};
  42. } // namespace boost
  43. #endif
  44. #else
  45. #include <boost/type_traits/detail/yes_no_type.hpp>
  46. #include <boost/type_traits/is_noncopyable.hpp>
  47. #include <boost/type_traits/add_reference.hpp>
  48. #include <boost/type_traits/is_rvalue_reference.hpp>
  49. #include <boost/type_traits/declval.hpp>
  50. #include <boost/type_traits/is_array.hpp>
  51. #include <boost/type_traits/declval.hpp>
  52. #ifdef BOOST_MSVC
  53. #pragma warning(push)
  54. #pragma warning(disable:4181)
  55. #endif
  56. namespace boost {
  57. namespace detail{
  58. template <bool DerivedFromNoncopyable, class T>
  59. struct is_copy_constructible_impl2 {
  60. // Intel compiler has problems with SFINAE for copy constructors and deleted functions:
  61. //
  62. // error: function *function_name* cannot be referenced -- it is a deleted function
  63. // static boost::type_traits::yes_type test(T1&, decltype(T1(boost::declval<T1&>()))* = 0);
  64. // ^
  65. //
  66. // MSVC 12.0 (Visual 2013) has problems when the copy constructor has been deleted. See:
  67. // https://connect.microsoft.com/VisualStudio/feedback/details/800328/std-is-copy-constructible-is-broken
  68. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) && !defined(BOOST_INTEL_CXX_VERSION) && !(defined(BOOST_MSVC) && _MSC_VER == 1800)
  69. #ifdef BOOST_NO_CXX11_DECLTYPE
  70. template <class T1>
  71. static boost::type_traits::yes_type test(const T1&, boost::mpl::int_<sizeof(T1(boost::declval<const T1&>()))>* = 0);
  72. #else
  73. template <class T1>
  74. static boost::type_traits::yes_type test(const T1&, decltype(T1(boost::declval<const T1&>()))* = 0);
  75. #endif
  76. static boost::type_traits::no_type test(...);
  77. #else
  78. template <class T1>
  79. static boost::type_traits::no_type test(const T1&, typename T1::boost_move_no_copy_constructor_or_assign* = 0);
  80. static boost::type_traits::yes_type test(...);
  81. #endif
  82. // If you see errors like this:
  83. //
  84. // `'T::T(const T&)' is private`
  85. // `boost/type_traits/is_copy_constructible.hpp:68:5: error: within this context`
  86. //
  87. // then you are trying to call that macro for a structure defined like that:
  88. //
  89. // struct T {
  90. // ...
  91. // private:
  92. // T(const T &);
  93. // ...
  94. // };
  95. //
  96. // To fix that you must modify your structure:
  97. //
  98. // // C++03 and C++11 version
  99. // struct T: private boost::noncopyable {
  100. // ...
  101. // private:
  102. // T(const T &);
  103. // ...
  104. // };
  105. //
  106. // // C++11 version
  107. // struct T {
  108. // ...
  109. // private:
  110. // T(const T &) = delete;
  111. // ...
  112. // };
  113. BOOST_STATIC_CONSTANT(bool, value = (
  114. sizeof(test(
  115. boost::declval<BOOST_DEDUCED_TYPENAME boost::add_reference<T const>::type>()
  116. )) == sizeof(boost::type_traits::yes_type)
  117. &&
  118. !boost::is_rvalue_reference<T>::value
  119. && !boost::is_array<T>::value
  120. ));
  121. };
  122. template <class T>
  123. struct is_copy_constructible_impl2<true, T> {
  124. BOOST_STATIC_CONSTANT(bool, value = false);
  125. };
  126. template <class T>
  127. struct is_copy_constructible_impl {
  128. BOOST_STATIC_CONSTANT(bool, value = (
  129. boost::detail::is_copy_constructible_impl2<
  130. boost::is_noncopyable<T>::value,
  131. T
  132. >::value
  133. ));
  134. };
  135. } // namespace detail
  136. template <class T> struct is_copy_constructible : public integral_constant<bool, ::boost::detail::is_copy_constructible_impl<T>::value>{};
  137. template <> struct is_copy_constructible<void> : public false_type{};
  138. #ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS
  139. template <> struct is_copy_constructible<void const> : public false_type{};
  140. template <> struct is_copy_constructible<void volatile> : public false_type{};
  141. template <> struct is_copy_constructible<void const volatile> : public false_type{};
  142. #endif
  143. } // namespace boost
  144. #ifdef BOOST_MSVC
  145. #pragma warning(pop)
  146. #endif
  147. #endif
  148. #endif // BOOST_TT_IS_COPY_CONSTRUCTIBLE_HPP_INCLUDED