Made a stab at has_copy_constructor. Got it mostly working for g++-4.0, but only works for scalar types on clang. Ultimately this needs a compiler-supported is_constructible which clang is missing, and won't be able to use until it gets variadic templates.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@113304 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant 2010-09-07 23:11:28 +00:00
parent aad0db393f
commit 954b366317
3 changed files with 71 additions and 9 deletions

View File

@ -761,10 +761,6 @@ template <class _Tp> struct has_nothrow_copy_constructor
template <class _Tp> struct has_nothrow_move_constructor : public has_nothrow_copy_constructor<_Tp> {};
// has_copy_constructor
template <class _Tp> struct has_copy_constructor : public true_type {};
// has_copy_assign
template <class _Tp> struct has_copy_assign;
@ -1732,6 +1728,24 @@ struct __is_constructible0_imp<false, _A[]>
#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
// has_copy_constructor
#ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
template <class _Tp>
struct has_copy_constructor
: public is_constructible<_Tp, typename add_lvalue_reference<const _Tp>::type>
{};
#else // _LIBCPP_HAS_NO_ADVANCED_SFINAE
template <class _Tp>
struct has_copy_constructor
: public has_nothrow_copy_constructor<_Tp>
{};
#endif // _LIBCPP_HAS_NO_ADVANCED_SFINAE
template <class _Tp> struct __is_zero_default_constructible
: public integral_constant<bool, is_scalar<_Tp>::value || is_empty<_Tp>::value> {};

View File

@ -13,7 +13,55 @@
#include <type_traits>
template <class T, bool Result>
void test_has_copy_constructor()
{
static_assert(std::has_copy_constructor<T>::value == Result, "");
}
class Empty
{
};
class NotEmpty
{
public:
virtual ~NotEmpty();
};
union Union {};
struct bit_zero
{
int : 0;
};
class Abstract
{
public:
virtual ~Abstract() = 0;
};
struct A
{
A(const A&);
};
int main()
{
#error has_copy_constructor not implemented
test_has_copy_constructor<char[3], false>();
test_has_copy_constructor<char[], false>();
test_has_copy_constructor<void, false>();
test_has_copy_constructor<Abstract, false>();
test_has_copy_constructor<A, true>();
test_has_copy_constructor<int&, true>();
test_has_copy_constructor<Union, true>();
test_has_copy_constructor<Empty, true>();
test_has_copy_constructor<int, true>();
test_has_copy_constructor<double, true>();
test_has_copy_constructor<int*, true>();
test_has_copy_constructor<const int*, true>();
test_has_copy_constructor<NotEmpty, true>();
test_has_copy_constructor<bit_zero, true>();
}

View File

@ -61,11 +61,11 @@ struct A
int main()
{
// test_has_not_nothrow_copy_constructor<void>();
test_has_not_nothrow_copy_constructor<void>();
test_has_not_nothrow_copy_constructor<A>();
test_has_not_nothrow_copy_constructor<Abstract>();
// test_has_not_nothrow_copy_constructor<char[3]>();
// test_has_not_nothrow_copy_constructor<char[]>();
test_has_not_nothrow_copy_constructor<char[3]>();
test_has_not_nothrow_copy_constructor<char[]>();
test_has_nothrow_copy_constructor<int&>();
test_has_nothrow_copy_constructor<Union>();
@ -74,6 +74,6 @@ int main()
test_has_nothrow_copy_constructor<double>();
test_has_nothrow_copy_constructor<int*>();
test_has_nothrow_copy_constructor<const int*>();
// test_has_nothrow_copy_constructor<NotEmpty>();
test_has_nothrow_copy_constructor<NotEmpty>();
test_has_nothrow_copy_constructor<bit_zero>();
}