forked from boostorg/type_traits
Improve is_constructible.
Add is_destructible. Add tests.
This commit is contained in:
@ -14,6 +14,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/detail/yes_no_type.hpp>
|
||||
#include <boost/type_traits/detail/decl.hpp>
|
||||
|
||||
@ -25,14 +26,26 @@ namespace boost{
|
||||
{
|
||||
template<typename T, typename ...TheArgs, typename = decltype(T(tt_declval<TheArgs>()...))>
|
||||
static boost::type_traits::yes_type test(int);
|
||||
|
||||
template<typename, typename>
|
||||
template<typename, typename...>
|
||||
static boost::type_traits::no_type test(...);
|
||||
|
||||
template<typename T, typename Arg, typename = decltype(::new T(tt_declval<Arg>()))>
|
||||
static boost::type_traits::yes_type test1(int);
|
||||
template<typename, typename>
|
||||
static boost::type_traits::no_type test1(...);
|
||||
|
||||
template <typename T>
|
||||
static boost::type_traits::yes_type ref_test(T);
|
||||
template <typename T>
|
||||
static boost::type_traits::no_type ref_test(...);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
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)>{};
|
||||
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)>{};
|
||||
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)>{};
|
||||
|
||||
#else
|
||||
|
||||
|
49
include/boost/type_traits/is_destructible.hpp
Normal file
49
include/boost/type_traits/is_destructible.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
|
||||
// (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_DESTRUCTIBLE_HPP_INCLUDED
|
||||
#define BOOST_TT_IS_DESTRUCTIBLE_HPP_INCLUDED
|
||||
|
||||
#include <boost/type_traits/integral_constant.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#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_destructible_imp
|
||||
{
|
||||
template<typename T, typename = decltype(tt_declval<T&>().~T())>
|
||||
static boost::type_traits::yes_type test(int);
|
||||
template<typename>
|
||||
static boost::type_traits::no_type test(...);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template <class T> struct is_destructible : public integral_constant<bool, sizeof(detail::is_destructible_imp::test<T>(0)) == sizeof(boost::type_traits::yes_type)>{};
|
||||
|
||||
#else
|
||||
|
||||
#include <boost/type_traits/is_pod.hpp>
|
||||
#include <boost/type_traits/is_class.hpp>
|
||||
|
||||
namespace boost{
|
||||
|
||||
// We don't know how to implement this:
|
||||
template <class T> struct is_destructible : public integral_constant<bool, is_pod<T>::value || is_class<T>::value>{};
|
||||
#endif
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_TT_IS_DESTRUCTIBLE_HPP_INCLUDED
|
79
test/is_constructible_test.cpp
Normal file
79
test/is_constructible_test.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
|
||||
// (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_constructible.hpp>
|
||||
#endif
|
||||
|
||||
|
||||
struct non_copy_constructible
|
||||
{
|
||||
non_copy_constructible();
|
||||
non_copy_constructible(int);
|
||||
non_copy_constructible(double*, double*);
|
||||
#ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
|
||||
non_copy_constructible(const non_copy_constructible&) = delete;
|
||||
#endif
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
non_copy_constructible(non_copy_constructible&&);
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
struct A { };
|
||||
struct B : A { };
|
||||
|
||||
TT_TEST_BEGIN(is_constructible)
|
||||
|
||||
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B &&, A>::value) , false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B const &&, A>::value) , false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B const &&, A const>::value) , false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B volatile &&, A>::value) , false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B volatile &&, A volatile>::value) , false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B const volatile &&, A>::value) , false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B const volatile &&, A const>::value) , false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B const volatile &&, A volatile>::value) , false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B const volatile &&, A const volatile>::value) , false);
|
||||
#endif
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B&, A>::value), false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B const &, A>::value), false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B const &, A&>::value), false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<B const &, A const>::value), false);
|
||||
|
||||
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_constructible<non_copy_constructible>::value), true, false);
|
||||
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_constructible<non_copy_constructible, int>::value), true, false);
|
||||
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_constructible<non_copy_constructible, int const>::value), true, false);
|
||||
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_constructible<non_copy_constructible, int const&>::value), true, false);
|
||||
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_constructible<non_copy_constructible, non_copy_constructible>::value), true, false);
|
||||
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !BOOST_WORKAROUND(BOOST_MSVC, < 1800)
|
||||
BOOST_CHECK_SOFT_INTEGRAL_CONSTANT((::tt::is_constructible<non_copy_constructible, double*, double*>::value), true, false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<non_copy_constructible, double const*, double*>::value), false);
|
||||
#endif
|
||||
#ifndef BOOST_NO_CXX11_DELETED_FUNCTIONS
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<non_copy_constructible, const non_copy_constructible&>::value), false);
|
||||
#endif
|
||||
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int, const int>::value), true);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int, const int&>::value), true);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int*, const int&>::value), false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int*, const int*>::value), false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int*, int*>::value), true);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int*, int[2]>::value), true);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int*, int[]>::value), true);
|
||||
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int(*)(int), int&>::value), false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int(*)(int), int(*)(int)>::value), true);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int(&)(int), int(int)>::value), true);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int(int), int(int)>::value), false);
|
||||
BOOST_CHECK_INTEGRAL_CONSTANT((::tt::is_constructible<int(int), int(&)(int)>::value), false);
|
||||
|
||||
TT_TEST_END
|
||||
|
Reference in New Issue
Block a user