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