Add is_assignable.

Update has_nothrow_assign to make use of it.
This commit is contained in:
jzmaddock
2015-06-06 12:51:51 +01:00
parent 34805a673f
commit acf6e41edb
6 changed files with 275 additions and 12 deletions

View File

@@ -41,6 +41,7 @@
#include "boost/type_traits/has_trivial_move_constructor.hpp"
#include "boost/type_traits/has_virtual_destructor.hpp"
#include "boost/type_traits/is_abstract.hpp"
#include "boost/type_traits/is_assignable.hpp"
#include "boost/type_traits/is_arithmetic.hpp"
#include "boost/type_traits/is_array.hpp"
#include "boost/type_traits/is_base_and_derived.hpp"
@@ -52,6 +53,7 @@
#include "boost/type_traits/is_convertible.hpp"
#include "boost/type_traits/is_copy_constructible.hpp"
#include "boost/type_traits/is_copy_assignable.hpp"
#include "boost/type_traits/is_default_constructible.hpp"
#include "boost/type_traits/is_empty.hpp"
#include "boost/type_traits/is_enum.hpp"
#include "boost/type_traits/is_float.hpp"

View File

@@ -19,11 +19,14 @@
#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/is_volatile.hpp>
#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/is_assignable.hpp>
#include <boost/type_traits/add_reference.hpp>
#endif
#endif
#if defined(__GNUC__) || defined(__SUNPRO_CC)
#include <boost/type_traits/is_const.hpp>
#include <boost/type_traits/is_volatile.hpp>
#include <boost/type_traits/is_assignable.hpp>
#ifdef BOOST_INTEL
#include <boost/type_traits/is_pod.hpp>
#endif
@@ -35,28 +38,32 @@ namespace boost {
namespace detail
{
template <class T, bool b1> struct has_nothrow_assign_imp{ static const bool value = false; };
template <class T> struct has_nothrow_assign_imp<T, false>{ static const bool value = noexcept(detail::tt_decl_ref<T>() = detail::tt_decl_const_ref<T>()); };
template <class T, std::size_t N> struct has_nothrow_assign_imp<T[N], false>{ static const bool value = has_nothrow_assign_imp<T, false>::value; };
template <class T> struct has_nothrow_assign_imp<T[], false>{ static const bool value = has_nothrow_assign_imp<T, false>::value; };
template <class T, bool b1, bool b2> struct has_nothrow_assign_imp{ static const bool value = false; };
template <class T> struct has_nothrow_assign_imp<T, false, true>{ static const bool value = noexcept(detail::tt_decl_ref<T>() = detail::tt_decl_const_ref<T>()); };
template <class T, std::size_t N> struct has_nothrow_assign_imp<T[N], false, true>{ static const bool value = has_nothrow_assign_imp<T, false, true>::value; };
template <class T> struct has_nothrow_assign_imp<T[], false, true>{ static const bool value = has_nothrow_assign_imp<T, false, true>::value; };
}
#endif
template <class T>
struct has_nothrow_assign : public integral_constant<bool,
template <class T>
struct has_nothrow_assign : public integral_constant < bool,
#ifndef BOOST_HAS_NOTHROW_ASSIGN
#if !defined(BOOST_NO_CXX11_NOEXCEPT) && !defined(BOOST_NO_SFINAE_EXPR) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
// Portable C++11 version:
detail::has_nothrow_assign_imp<T, is_const<typename remove_reference<T>::type>::value || is_volatile<typename remove_reference<T>::type>::value || is_reference<T>::value>::value
// Portable C++11 version:
detail::has_nothrow_assign_imp<T,
(is_const<typename remove_reference<T>::type>::value || is_volatile<typename remove_reference<T>::type>::value || is_reference<T>::value),
is_assignable<typename add_reference<T>::type, typename add_reference<const T>::type>::value
>::value
#else
::boost::has_trivial_assign<T>::value
::boost::has_trivial_assign<T>::value
#endif
#else
BOOST_HAS_NOTHROW_ASSIGN(T)
BOOST_HAS_NOTHROW_ASSIGN(T)
#endif
>{};
> {};
template <class T, std::size_t N> struct has_nothrow_assign <T[N]> : public has_nothrow_assign<T> {};
template <> struct has_nothrow_assign<void> : public false_type{};
#ifndef BOOST_NO_CV_VOID_SPECIALIZATIONS
template <> struct has_nothrow_assign<void const> : public false_type{};

View File

@@ -245,7 +245,7 @@
# define BOOST_HAS_TRIVIAL_DESTRUCTOR(T) (__has_trivial_destructor(T) BOOST_INTEL_TT_OPTS)
# define BOOST_HAS_NOTHROW_CONSTRUCTOR(T) (__has_nothrow_constructor(T) && is_default_constructible<T>::value BOOST_INTEL_TT_OPTS)
# define BOOST_HAS_NOTHROW_COPY(T) ((__has_nothrow_copy(T) BOOST_INTEL_TT_OPTS) && !is_volatile<T>::value && !is_reference<T>::value)
# define BOOST_HAS_NOTHROW_ASSIGN(T) ((__has_nothrow_assign(T) BOOST_INTEL_TT_OPTS) && !is_volatile<T>::value && !is_const<T>::value)
# define BOOST_HAS_NOTHROW_ASSIGN(T) ((__has_nothrow_assign(T) BOOST_INTEL_TT_OPTS) && !is_volatile<T>::value && !is_const<T>::value && is_assignable<T&, const T&>::value)
# define BOOST_HAS_VIRTUAL_DESTRUCTOR(T) __has_virtual_destructor(T)
# define BOOST_IS_ABSTRACT(T) __is_abstract(T)

View File

@@ -0,0 +1,76 @@
// (C) Copyright John Maddock 2015.
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt).
//
// See http://www.boost.org/libs/type_traits for most recent version including documentation.
#ifndef BOOST_TT_IS_ASSIGNABLE_HPP_INCLUDED
#define BOOST_TT_IS_ASSIGNABLE_HPP_INCLUDED
#include <boost/type_traits/integral_constant.hpp>
#include <boost/detail/workaround.hpp>
namespace boost{
template <class T, class U = T> struct is_assignable;
}
#if !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800)
#include <boost/type_traits/detail/yes_no_type.hpp>
#include <boost/type_traits/detail/decl.hpp>
namespace boost{
namespace detail{
struct is_assignable_imp
{
template<typename T, typename U, typename = decltype(tt_declval<T>() = tt_declval<U>())>
static boost::type_traits::yes_type test(int);
template<typename, typename>
static boost::type_traits::no_type test(...);
};
}
template <class T, class U> struct is_assignable : public integral_constant<bool, sizeof(detail::is_assignable_imp::test<T, U>(0)) == sizeof(boost::type_traits::yes_type)>{};
template <class T, std::size_t N, class U> struct is_assignable<T[N], U> : public is_assignable<T, U>{};
template <class T, std::size_t N, class U> struct is_assignable<T(&)[N], U> : public is_assignable<T&, U>{};
template <class T, class U> struct is_assignable<T[], U> : public is_assignable<T, U>{};
template <class T, class U> struct is_assignable<T(&)[], U> : public is_assignable<T&, U>{};
template <class U> struct is_assignable<void, U> : public integral_constant<bool, false>{};
template <class U> struct is_assignable<void const, U> : public integral_constant<bool, false>{};
template <class U> struct is_assignable<void volatile, U> : public integral_constant<bool, false>{};
template <class U> struct is_assignable<void const volatile, U> : public integral_constant<bool, false>{};
#else
#include <boost/type_traits/has_trivial_assign.hpp>
#include <boost/type_traits/remove_reference.hpp>
namespace boost{
// We don't know how to implement this:
template <class T, class U> struct is_assignable : public integral_constant<bool, false>{};
template <class T, class U> struct is_assignable<T&, U> : public integral_constant<bool, is_pod<T>::value && is_pod<typename remove_reference<U>::type>::value>{};
template <class T, class U> struct is_assignable<const T&, U> : public integral_constant<bool, false>{};
template <class U> struct is_assignable<void, U> : public integral_constant<bool, false>{};
template <class U> struct is_assignable<void const, U> : public integral_constant<bool, false>{};
template <class U> struct is_assignable<void volatile, U> : public integral_constant<bool, false>{};
template <class U> struct is_assignable<void const volatile, U> : public integral_constant<bool, false>{};
/*
template <> struct is_assignable<void, void> : public integral_constant<bool, false>{};
template <> struct is_assignable<void const, void const> : public integral_constant<bool, false>{};
template <> struct is_assignable<void volatile, void volatile> : public integral_constant<bool, false>{};
template <> struct is_assignable<void const volatile, void const volatile> : public integral_constant<bool, false>{};
*/
#endif
} // namespace boost
#endif // BOOST_TT_IS_ASSIGNABLE_HPP_INCLUDED

View File

@@ -12,6 +12,26 @@
# include <boost/type_traits/has_nothrow_assign.hpp>
#endif
#ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
struct non_assignable
{
non_assignable();
non_assignable& operator=(const non_assignable&) = delete;
};
#endif
#ifndef BOOST_NO_CXX11_NOEXCEPT
struct noexcept_assignable
{
noexcept_assignable();
noexcept_assignable& operator=(const non_assignable&)noexcept;
};
#endif
TT_TEST_BEGIN(has_nothrow_assign)
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::has_nothrow_assign<bool>::value, true);
@@ -205,6 +225,13 @@ BOOST_CHECK_INTEGRAL_CONSTANT(::tt::has_nothrow_assign<nothrow_construct_UDT>::v
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::has_nothrow_assign<test_abc1>::value, false);
#ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::has_nothrow_assign<non_assignable>::value, false);
#endif
#ifndef BOOST_NO_CXX11_NOEXCEPT
BOOST_CHECK_INTEGRAL_CONSTANT(::tt::has_nothrow_assign<noexcept_assignable>::value, true);
#endif
TT_TEST_END

151
test/is_assignable_test.cpp Normal file
View File

@@ -0,0 +1,151 @@
// (C) Copyright John Maddock 2000.
// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "test.hpp"
#include "check_integral_constant.hpp"
#ifdef TEST_STD
# include <type_traits>
#else
# include <boost/type_traits/is_assignable.hpp>
#endif
#ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
struct non_assignable
{
non_assignable();
non_assignable& operator=(const non_assignable&) = delete;
};
#endif
#ifndef BOOST_NO_CXX11_NOEXCEPT
struct noexcept_assignable
{
noexcept_assignable();
noexcept_assignable& operator=(const non_assignable&)noexcept;
};
#endif
TT_TEST_BEGIN(is_assignable)
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<bool&, const bool&>::value), true);
#ifndef TEST_STD
// unspecified behaviour:
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<bool const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<bool volatile&, bool const volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<bool const volatile&>::value), false);
#endif
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<signed char&, const signed char&>::value), true);
#ifndef TEST_STD
// unspecified behaviour:
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<signed char const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<signed char volatile&, const signed char volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<signed char const volatile&>::value), false);
#endif
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned char&, const unsigned char&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<char&, const char&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned short&, const unsigned short&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<short&, const short&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned int&, const unsigned int&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<int&, const int&>::value), true);
#ifndef TEST_STD
// unspecified behaviour:
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned int const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<int const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned int volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<int volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned int const volatile&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<int const volatile&>::value), false);
#endif
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned long&, const unsigned long&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<long&, const long&>::value), true);
#ifndef TEST_STD
// unspecified behaviour:
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned long const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<long const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned long volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<long volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<unsigned long const volatile&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<long const volatile&>::value), false);
#endif
#ifdef BOOST_HAS_LONG_LONG
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable< ::boost::ulong_long_type&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable< ::boost::long_long_type&>::value), true);
#ifndef TEST_STD
// unspecified behaviour:
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable< ::boost::ulong_long_type const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable< ::boost::long_long_type const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable< ::boost::ulong_long_type volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable< ::boost::long_long_type volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable< ::boost::ulong_long_type const volatile&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable< ::boost::long_long_type const volatile&>::value), false);
#endif
#endif
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<float&, const float&>::value), true);
#ifndef TEST_STD
// unspecified behaviour:
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<float const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<float volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<float const volatile&>::value), false);
#endif
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<double&, const double&>::value), true);
#ifndef TEST_STD
// unspecified behaviour:
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<double const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<double volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<double const volatile&>::value), false);
#endif
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<long double&, const long double&>::value), true);
#ifndef TEST_STD
// unspecified behaviour:
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<long double const&>::value), false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<long double volatile&>::value), true);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<long double const volatile&>::value), false);
#endif
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<void*&>::value), true);
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<int&, int&&>::value), true);
#endif
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<const int&>::value), false);
//BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<int(&)[2], const int(&)[2]>::value), true);
//BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<int(&)[3][2]>::value), true);
//BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<int(&)[2][4][5][6][3]>::value), true);
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_assignable<UDT&>::value), true, false);
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_assignable<empty_UDT&>::value), true, false);
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<void>::value), false);
// cases we would like to succeed but can't implement in the language:
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_assignable<empty_POD_UDT&>::value), true, false);
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_assignable<POD_UDT&>::value), true, false);
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_assignable<POD_union_UDT&>::value), true, false);
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_assignable<empty_POD_union_UDT&>::value), true, false);
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_assignable<nothrow_assign_UDT&>::value), true, false);
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_assignable<test_abc1&>::value), true, false);
#ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<non_assignable&>::value), false);
#endif
#ifndef BOOST_NO_CXX11_NOEXCEPT
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_assignable<noexcept_assignable&>::value), true);
#endif
TT_TEST_END