Change is_copy_constructible to use is_constructible where possible, and fix some MSVC failures.

This commit is contained in:
jzmaddock
2015-06-12 13:26:27 +01:00
parent ae76223bab
commit cb98a630ff
3 changed files with 155 additions and 86 deletions

View File

@ -15,6 +15,7 @@
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800)
#include <boost/type_traits/is_destructible.hpp>
#include <boost/type_traits/is_default_constructible.hpp>
#include <boost/type_traits/detail/yes_no_type.hpp>
#include <boost/type_traits/detail/decl.hpp>
@ -47,6 +48,13 @@ namespace boost{
template <class Ref, class Arg> struct is_constructible<Ref&, Arg> : public integral_constant<bool, sizeof(detail::is_constructible_imp::ref_test<Ref&>(detail::tt_declval<Arg>())) == sizeof(boost::type_traits::yes_type)>{};
template <class Ref, class Arg> struct is_constructible<Ref&&, Arg> : public integral_constant<bool, sizeof(detail::is_constructible_imp::ref_test<Ref&&>(detail::tt_declval<Arg>())) == sizeof(boost::type_traits::yes_type)>{};
template <> struct is_constructible<void> : public false_type{};
template <> struct is_constructible<void const> : public false_type{};
template <> struct is_constructible<void const volatile> : public false_type{};
template <> struct is_constructible<void volatile> : public false_type{};
template <class T> struct is_constructible<T> : public is_default_constructible<T>{};
#else
#include <boost/type_traits/is_convertible.hpp>
@ -57,6 +65,14 @@ namespace boost{
// We don't know how to implement this:
template <class T, class U = void> struct is_constructible : public is_convertible<U, T>{};
template <class T> struct is_constructible<T, void> : public is_default_constructible<T>{};
template <> struct is_constructible<void, void> : public false_type{};
template <> struct is_constructible<void const, void> : public false_type{};
template <> struct is_constructible<void const volatile, void> : public false_type{};
template <> struct is_constructible<void volatile, void> : public false_type{};
template <class Ref> struct is_constructible<Ref&, void> : public false_type{};
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
template <class Ref> struct is_constructible<Ref&&, void> : public false_type{};
#endif
#endif
} // namespace boost

View File

@ -9,111 +9,170 @@
#ifndef BOOST_TT_IS_COPY_CONSTRUCTIBLE_HPP_INCLUDED
#define BOOST_TT_IS_COPY_CONSTRUCTIBLE_HPP_INCLUDED
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800)
#include <boost/type_traits/is_constructible.hpp>
#if !BOOST_WORKAROUND(BOOST_MSVC, <= 1800)
namespace boost {
template <class T> struct is_copy_constructible : public boost::is_constructible<T, const T&>{};
template <> struct is_copy_constructible<void> : public false_type{};
template <> struct is_copy_constructible<void const> : public false_type{};
template <> struct is_copy_constructible<void const volatile> : public false_type{};
template <> struct is_copy_constructible<void volatile> : public false_type{};
} // namespace boost
#else
//
// Special version for VC12 which has a problem when a base class (such as non_copyable) has a deleted
// copy constructor. In this case the compiler thinks there really is a copy-constructor and tries to
// instantiate the deleted member. std::is_copy_constructible has the same issue (or at least returns
// an incorrect value, which just defers the issue into the users code) as well. We can at least fix
// boost::non_copyable as a base class as a special case:
//
#include <boost/type_traits/is_base_and_derived.hpp>
#include <boost/noncopyable.hpp>
namespace boost {
namespace detail
{
template <class T, bool b> struct is_copy_constructible_imp : public boost::is_constructible<T, const T&>{};
template <class T> struct is_copy_constructible_imp<T, true> : public false_type{};
}
template <class T> struct is_copy_constructible : public detail::is_copy_constructible_imp<T, is_base_and_derived<boost::noncopyable, T>::value>{};
template <> struct is_copy_constructible<void> : public false_type{};
template <> struct is_copy_constructible<void const> : public false_type{};
template <> struct is_copy_constructible<void const volatile> : public false_type{};
template <> struct is_copy_constructible<void volatile> : public false_type{};
} // namespace boost
#endif
#else
#include <boost/type_traits/detail/yes_no_type.hpp>
#include <boost/type_traits/is_base_and_derived.hpp>
#include <boost/type_traits/add_reference.hpp>
#include <boost/type_traits/is_rvalue_reference.hpp>
#include <boost/type_traits/declval.hpp>
#include <boost/type_traits/is_array.hpp>
#include <boost/type_traits/detail/decl.hpp>
#include <boost/noncopyable.hpp>
namespace boost {
namespace detail{
namespace detail{
template <bool DerivedFromNoncopyable, class T>
struct is_copy_constructible_impl2 {
template <bool DerivedFromNoncopyable, class T>
struct is_copy_constructible_impl2 {
// Intel compiler has problems with SFINAE for copy constructors and deleted functions:
//
// error: function *function_name* cannot be referenced -- it is a deleted function
// static boost::type_traits::yes_type test(T1&, decltype(T1(boost::declval<T1&>()))* = 0);
// ^
//
// MSVC 12.0 (Visual 2013) has problems when the copy constructor has been deleted. See:
// https://connect.microsoft.com/VisualStudio/feedback/details/800328/std-is-copy-constructible-is-broken
// Intel compiler has problems with SFINAE for copy constructors and deleted functions:
//
// error: function *function_name* cannot be referenced -- it is a deleted function
// static boost::type_traits::yes_type test(T1&, decltype(T1(boost::declval<T1&>()))* = 0);
// ^
//
// MSVC 12.0 (Visual 2013) has problems when the copy constructor has been deleted. See:
// https://connect.microsoft.com/VisualStudio/feedback/details/800328/std-is-copy-constructible-is-broken
#if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) && !defined(BOOST_INTEL_CXX_VERSION) && !(defined(BOOST_MSVC) && _MSC_VER == 1800)
#ifdef BOOST_NO_CXX11_DECLTYPE
template <class T1>
static boost::type_traits::yes_type test(T1&, boost::mpl::int_<sizeof(T1(boost::declval<T1&>()))>* = 0);
template <class T1>
static boost::type_traits::yes_type test(const T1&, boost::mpl::int_<sizeof(T1(boost::declval<const T1&>()))>* = 0);
#else
template <class T1>
static boost::type_traits::yes_type test(T1&, decltype(T1(boost::declval<T1&>()))* = 0);
template <class T1>
static boost::type_traits::yes_type test(const T1&, decltype(T1(boost::declval<const T1&>()))* = 0);
#endif
static boost::type_traits::no_type test(...);
static boost::type_traits::no_type test(...);
#else
template <class T1>
static boost::type_traits::no_type test(T1&, typename T1::boost_move_no_copy_constructor_or_assign* = 0);
static boost::type_traits::yes_type test(...);
template <class T1>
static boost::type_traits::no_type test(const T1&, typename T1::boost_move_no_copy_constructor_or_assign* = 0);
static boost::type_traits::yes_type test(...);
#endif
// If you see errors like this:
//
// `'T::T(const T&)' is private`
// `boost/type_traits/is_copy_constructible.hpp:68:5: error: within this context`
//
// then you are trying to call that macro for a structure defined like that:
//
// struct T {
// ...
// private:
// T(const T &);
// ...
// };
//
// To fix that you must modify your structure:
//
// // C++03 and C++11 version
// struct T: private boost::noncopyable {
// ...
// private:
// T(const T &);
// ...
// };
//
// // C++11 version
// struct T {
// ...
// private:
// T(const T &) = delete;
// ...
// };
BOOST_STATIC_CONSTANT(bool, value = (
// If you see errors like this:
//
// `'T::T(const T&)' is private`
// `boost/type_traits/is_copy_constructible.hpp:68:5: error: within this context`
//
// then you are trying to call that macro for a structure defined like that:
//
// struct T {
// ...
// private:
// T(const T &);
// ...
// };
//
// To fix that you must modify your structure:
//
// // C++03 and C++11 version
// struct T: private boost::noncopyable {
// ...
// private:
// T(const T &);
// ...
// };
//
// // C++11 version
// struct T {
// ...
// private:
// T(const T &) = delete;
// ...
// };
BOOST_STATIC_CONSTANT(bool, value = (
sizeof(test(
boost::declval<BOOST_DEDUCED_TYPENAME boost::add_reference<T>::type>()
tt_declval</*BOOST_DEDUCED_TYPENAME boost::add_reference<T const>::type*/ const T&>()
)) == sizeof(boost::type_traits::yes_type)
||
boost::is_rvalue_reference<T>::value
));
};
&&
!boost::is_rvalue_reference<T>::value
&& !boost::is_array<T>::value
));
};
template <class T>
struct is_copy_constructible_impl2<true, T> {
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_copy_constructible_impl2<true, T> {
BOOST_STATIC_CONSTANT(bool, value = false);
};
template <class T>
struct is_copy_constructible_impl {
template <class T>
struct is_copy_constructible_impl {
BOOST_STATIC_CONSTANT(bool, value = (
boost::detail::is_copy_constructible_impl2<
BOOST_STATIC_CONSTANT(bool, value = (
boost::detail::is_copy_constructible_impl2<
boost::is_base_and_derived<boost::noncopyable, T>::value,
T
>::value
));
};
>::value
));
};
} // namespace detail
} // namespace detail
template <class T> struct is_copy_constructible : public integral_constant<bool, ::boost::detail::is_copy_constructible_impl<T>::value>{};
template <> struct is_copy_constructible<void> : public false_type{};
template <class T> struct is_copy_constructible : public integral_constant<bool, ::boost::detail::is_copy_constructible_impl<T>::value>{};
template <> struct is_copy_constructible<void> : public false_type{};
#ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS
template <> struct is_copy_constructible<void const> : public false_type{};
template <> struct is_copy_constructible<void volatile> : public false_type{};
template <> struct is_copy_constructible<void const volatile> : public false_type{};
template <> struct is_copy_constructible<void const> : public false_type{};
template <> struct is_copy_constructible<void volatile> : public false_type{};
template <> struct is_copy_constructible<void const volatile> : public false_type{};
#endif
} // namespace boost
#endif
#endif // BOOST_TT_IS_COPY_CONSTRUCTIBLE_HPP_INCLUDED

View File

@ -80,7 +80,10 @@ TT_TEST_BEGIN(is_copy_constructible)
// Main part of the test
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has>::value, true);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has2>::value, true);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has3>::value, true);
// Only constructible from has3& not from const-reference, this only works if we have decltype:
#if !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800)
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has3>::value, false);
#endif
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has4>::value, true);
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<has_not>::value, false);
#if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) && !defined(BOOST_INTEL_CXX_VERSION)
@ -260,18 +263,9 @@ BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<enum_UDT>::value, true
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int&>::value, true);
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
// Code like `int&& a = 10;` or
// struct nonc {
// nonc() = default;
// nonc(const nonc&) = delete;
// nonc(nonc&&) = delete;
// nonc& operator=(const nonc&) = delete;
// nonc& operator=(nonc&&) = delete;
// };
//
// nonc && a = nonc();
// is legal in C++11. so this trait MUST return true.
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int&&>::value, true);
// This is debatable, we used to insist this was true, but copy-constructibility
// implies copying a constant-object, and that isn't the case here:
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<int&&>::value, false);
#endif
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::is_copy_constructible<const int&>::value, true);