is_constructible.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // (C) Copyright John Maddock 2015.
  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_IS_CONSTRUCTIBLE_HPP_INCLUDED
  8. #define BOOST_TT_IS_CONSTRUCTIBLE_HPP_INCLUDED
  9. #include <boost/type_traits/integral_constant.hpp>
  10. #include <boost/detail/workaround.hpp>
  11. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800) && !BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40500)
  12. #include <boost/type_traits/is_destructible.hpp>
  13. #include <boost/type_traits/is_default_constructible.hpp>
  14. #include <boost/type_traits/detail/yes_no_type.hpp>
  15. #include <boost/type_traits/declval.hpp>
  16. #include <boost/type_traits/is_complete.hpp>
  17. #include <boost/static_assert.hpp>
  18. #define BOOST_TT_IS_CONSTRUCTIBLE_CONFORMING 1
  19. namespace boost{
  20. namespace detail{
  21. struct is_constructible_imp
  22. {
  23. template<typename T, typename ...TheArgs, typename = decltype(T(boost::declval<TheArgs>()...))>
  24. static boost::type_traits::yes_type test(int);
  25. template<typename, typename...>
  26. static boost::type_traits::no_type test(...);
  27. template<typename T, typename Arg, typename = decltype(::new T(boost::declval<Arg>()))>
  28. static boost::type_traits::yes_type test1(int);
  29. template<typename, typename>
  30. static boost::type_traits::no_type test1(...);
  31. template <typename T>
  32. static boost::type_traits::yes_type ref_test(T);
  33. template <typename T>
  34. static boost::type_traits::no_type ref_test(...);
  35. };
  36. }
  37. template <class T, class ...Args> struct is_constructible : public integral_constant<bool, sizeof(detail::is_constructible_imp::test<T, Args...>(0)) == sizeof(boost::type_traits::yes_type)>
  38. {
  39. BOOST_STATIC_ASSERT_MSG(::boost::is_complete<T>::value, "The target type must be complete in order to test for constructibility");
  40. };
  41. template <class T, class Arg> struct is_constructible<T, Arg> : public integral_constant<bool, is_destructible<T>::value && sizeof(boost::detail::is_constructible_imp::test1<T, Arg>(0)) == sizeof(boost::type_traits::yes_type)>
  42. {
  43. BOOST_STATIC_ASSERT_MSG(::boost::is_complete<T>::value, "The target type must be complete in order to test for constructibility");
  44. };
  45. template <class Ref, class Arg> struct is_constructible<Ref&, Arg> : public integral_constant<bool, sizeof(detail::is_constructible_imp::ref_test<Ref&>(boost::declval<Arg>())) == sizeof(boost::type_traits::yes_type)>{};
  46. template <class Ref, class Arg> struct is_constructible<Ref&&, Arg> : public integral_constant<bool, sizeof(detail::is_constructible_imp::ref_test<Ref&&>(boost::declval<Arg>())) == sizeof(boost::type_traits::yes_type)>{};
  47. template <> struct is_constructible<void> : public false_type{};
  48. template <> struct is_constructible<void const> : public false_type{};
  49. template <> struct is_constructible<void const volatile> : public false_type{};
  50. template <> struct is_constructible<void volatile> : public false_type{};
  51. template <class T> struct is_constructible<T> : public is_default_constructible<T>{};
  52. #else
  53. #include <boost/type_traits/is_convertible.hpp>
  54. #include <boost/type_traits/is_default_constructible.hpp>
  55. namespace boost{
  56. // We don't know how to implement this:
  57. template <class T, class U = void> struct is_constructible : public is_convertible<U, T>{};
  58. template <class T> struct is_constructible<T, void> : public is_default_constructible<T>{};
  59. template <> struct is_constructible<void, void> : public false_type{};
  60. template <> struct is_constructible<void const, void> : public false_type{};
  61. template <> struct is_constructible<void const volatile, void> : public false_type{};
  62. template <> struct is_constructible<void volatile, void> : public false_type{};
  63. template <class Ref> struct is_constructible<Ref&, void> : public false_type{};
  64. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  65. template <class Ref> struct is_constructible<Ref&&, void> : public false_type{};
  66. #endif
  67. #endif
  68. } // namespace boost
  69. #endif // BOOST_TT_IS_CONSTRUCTIBLE_HPP_INCLUDED