A much improved type_traits for C++0x. Not yet done: is_trivially_constructible, is_trivially_assignable and underlying_type.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@131291 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant 2011-05-13 14:08:16 +00:00
parent 0cfa1f7cbd
commit 6063ec176d
18 changed files with 156 additions and 55 deletions

View File

@ -129,7 +129,6 @@ namespace std
template <class T> struct alignment_of;
template <size_t Len, size_t Align = most_stringent_alignment_requirement>
struct aligned_storage;
template <std::size_t Len, class... Types> struct aligned_union;
template <class T> struct decay;
template <class... T> struct common_type;
@ -169,11 +168,15 @@ struct _LIBCPP_VISIBLE integral_constant
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
constexpr
#endif
operator value_type() const {return value;}
operator value_type()
#ifdef _LIBCPP_HAS_NO_CONSTEXPR
const
#endif
{return value;}
};
template <class _Tp, _Tp __v>
const _Tp integral_constant<_Tp, __v>::value;
constexpr _Tp integral_constant<_Tp, __v>::value;
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
@ -1200,7 +1203,7 @@ decltype((_STD::declval<_Tp>() = _STD::declval<_Arg>(), true_type()))
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
__is_assignable_test(_Tp&&, _Arg&&);
#else
__is_assignable_test(_Tp&, _Arg&);
__is_assignable_test(_Tp, _Arg&);
#endif
template <class _Arg>
@ -1211,13 +1214,19 @@ __is_assignable_test(__any, _Arg&&);
__is_assignable_test(__any, _Arg&);
#endif
template <class _Tp, class _Arg>
template <class _Tp, class _Arg, bool = is_void<_Tp>::value || is_void<_Arg>::value>
struct __is_assignable_imp
: public common_type
<
decltype(__is_assignable_test(declval<_Tp>(), declval<_Arg>()))
>::type {};
template <class _Tp, class _Arg>
struct __is_assignable_imp<_Tp, _Arg, true>
: public false_type
{
};
template <class _Tp, class _Arg>
struct is_assignable
: public __is_assignable_imp<_Tp, _Arg> {};
@ -1225,13 +1234,15 @@ struct is_assignable
// is_copy_assignable
template <class _Tp> struct _LIBCPP_VISIBLE is_copy_assignable
: public is_assignable<_Tp&, const _Tp&> {};
: public is_assignable<typename add_lvalue_reference<_Tp>::type,
const typename add_lvalue_reference<_Tp>::type> {};
// is_move_assignable
template <class _Tp> struct _LIBCPP_VISIBLE is_move_assignable
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
: public is_assignable<_Tp&, _Tp&&> {};
: public is_assignable<typename add_lvalue_reference<_Tp>::type,
const typename add_rvalue_reference<_Tp>::type> {};
#else
: public is_copy_assignable<_Tp> {};
#endif
@ -2143,6 +2154,12 @@ struct _LIBCPP_VISIBLE is_nothrow_constructible
{
};
template <class _Tp, size_t _Ns>
struct _LIBCPP_VISIBLE is_nothrow_constructible<_Tp[_Ns]>
: __is_nothrow_constructible<is_constructible<_Tp>::value, _Tp>
{
};
#else // __has_feature(cxx_noexcept)
template <class _Tp, class... _Args>
@ -2384,6 +2401,28 @@ struct _LIBCPP_VISIBLE is_nothrow_destructible
{
};
template <class _Tp, size_t _Ns>
struct _LIBCPP_VISIBLE is_nothrow_destructible<_Tp[_Ns]>
: public is_nothrow_destructible<_Tp>
{
};
template <class _Tp>
struct _LIBCPP_VISIBLE is_nothrow_destructible<_Tp&>
: public true_type
{
};
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
template <class _Tp>
struct _LIBCPP_VISIBLE is_nothrow_destructible<_Tp&&>
: public true_type
{
};
#endif
#else
template <class _Tp> struct __libcpp_nothrow_destructor
@ -2423,6 +2462,37 @@ template <class _Tp> struct _LIBCPP_VISIBLE is_literal_type
#endif
{};
// is_standard_layout;
template <class _Tp> struct _LIBCPP_VISIBLE is_standard_layout
#if __has_feature(is_standard_layout)
: public integral_constant<bool, __is_standard_layout(_Tp)>
#else
: integral_constant<bool, is_scalar<typename remove_all_extents<_Tp>::type>::value>
#endif
{};
// is_trivially_copyable;
template <class _Tp> struct _LIBCPP_VISIBLE is_trivially_copyable
#if __has_feature(is_trivially_copyable)
: public integral_constant<bool, __is_trivially_copyable(_Tp)>
#else
: integral_constant<bool, is_scalar<typename remove_all_extents<_Tp>::type>::value>
#endif
{};
// is_trivial;
template <class _Tp> struct _LIBCPP_VISIBLE is_trivial
#if __has_feature(is_trivial)
: public integral_constant<bool, __is_trivial(_Tp)>
#else
: integral_constant<bool, is_trivially_copyable<_Tp>::value &&
is_trivially_default_constructible<T>::value>::value>
#endif
{};
template <class _Tp>
inline _LIBCPP_INLINE_VISIBILITY
void

View File

@ -20,7 +20,11 @@ int main()
static_assert(_5::value == 5, "");
static_assert((std::is_same<_5::value_type, int>::value), "");
static_assert((std::is_same<_5::type, _5>::value), "");
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
static_assert((_5() == 5), "");
#else
assert(_5() == 5);
#endif
static_assert(std::false_type::value == false, "");
static_assert((std::is_same<std::false_type::value_type, bool>::value), "");

View File

@ -1,19 +0,0 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// type_traits
// aligned_union
#include <type_traits>
int main()
{
#error aligned_union is not implemented
}

View File

@ -31,4 +31,8 @@ int main()
static_assert(( std::is_assignable<int&, double>::value), "");
static_assert(( std::is_assignable<B, A>::value), "");
static_assert((!std::is_assignable<A, B>::value), "");
static_assert((!std::is_assignable<void, const void>::value), "");
static_assert((!std::is_assignable<const void, const void>::value), "");
static_assert(( std::is_assignable<void*&, void*>::value), "");
static_assert((!std::is_assignable<int(), int>::value), "");
}

View File

@ -18,6 +18,8 @@ struct A
{
explicit A(int);
A(int, double);
private:
A(char);
};
int main()
@ -27,4 +29,9 @@ int main()
static_assert((std::is_constructible<A, int>::value), "");
static_assert((std::is_constructible<A, int, double>::value), "");
static_assert((!std::is_constructible<A>::value), "");
static_assert((!std::is_constructible<A, char>::value), "");
static_assert((!std::is_constructible<A, void>::value), "");
static_assert((!std::is_constructible<void>::value), "");
static_assert((!std::is_constructible<int&>::value), "");
static_assert(( std::is_constructible<int&, int&>::value), "");
}

View File

@ -35,6 +35,11 @@ struct A
A();
};
class B
{
B& operator=(const B&);
};
int main()
{
static_assert(( std::is_copy_assignable<int>::value), "");
@ -47,4 +52,6 @@ int main()
static_assert(( std::is_copy_assignable<Union>::value), "");
static_assert(( std::is_copy_assignable<NotEmpty>::value), "");
static_assert(( std::is_copy_assignable<Empty>::value), "");
static_assert((!std::is_copy_assignable<B>::value), "");
static_assert((!std::is_copy_assignable<void>::value), "");
}

View File

@ -47,6 +47,11 @@ struct A
A(const A&);
};
class B
{
B(const B&);
};
int main()
{
test_is_copy_constructible<char[3], false>();
@ -55,6 +60,7 @@ int main()
test_is_copy_constructible<Abstract, false>();
test_is_copy_constructible<A, true>();
test_is_copy_constructible<B, false>();
test_is_copy_constructible<int&, true>();
test_is_copy_constructible<Union, true>();
test_is_copy_constructible<Empty, true>();

View File

@ -50,6 +50,11 @@ struct A
A();
};
class B
{
B();
};
int main()
{
test_is_default_constructible<void, false>();
@ -58,6 +63,7 @@ int main()
test_is_default_constructible<Abstract, false>();
test_is_default_constructible<A, true>();
test_is_default_constructible<B, false>();
test_is_default_constructible<Union, true>();
test_is_default_constructible<Empty, true>();
test_is_default_constructible<int, true>();

View File

@ -13,10 +13,21 @@
#include <type_traits>
struct A
{
};
struct B
{
B();
};
int main()
{
static_assert( std::is_literal_type<int>::value, "");
static_assert( std::is_literal_type<const int>::value, "");
static_assert(!std::is_literal_type<int&>::value, "");
static_assert(!std::is_literal_type<volatile int&>::value, "");
static_assert( std::is_literal_type<int&>::value, "");
static_assert( std::is_literal_type<volatile int&>::value, "");
static_assert( std::is_literal_type<A>::value, "");
static_assert(!std::is_literal_type<B>::value, "");
}

View File

@ -41,7 +41,8 @@ int main()
static_assert((!std::is_move_assignable<const int>::value), "");
static_assert((!std::is_move_assignable<int[]>::value), "");
static_assert((!std::is_move_assignable<int[3]>::value), "");
static_assert(( std::is_move_assignable<int&>::value), "");
static_assert((!std::is_move_assignable<int[3]>::value), "");
static_assert((!std::is_move_assignable<void>::value), "");
static_assert(( std::is_move_assignable<A>::value), "");
static_assert(( std::is_move_assignable<bit_zero>::value), "");
static_assert(( std::is_move_assignable<Union>::value), "");

View File

@ -47,4 +47,6 @@ int main()
static_assert((!std::is_nothrow_constructible<A, int>::value), "");
static_assert((!std::is_nothrow_constructible<A, int, double>::value), "");
static_assert((!std::is_nothrow_constructible<A>::value), "");
static_assert(( std::is_nothrow_constructible<Empty>::value), "");
static_assert(( std::is_nothrow_constructible<Empty, const Empty&>::value), "");
}

View File

@ -42,6 +42,7 @@ struct A
int main()
{
test_has_nothrow_assign<const int, false>();
test_has_nothrow_assign<void, false>();
test_has_nothrow_assign<A, false>();
test_has_nothrow_assign<int&, true>();

View File

@ -18,8 +18,6 @@ void test_is_nothrow_copy_constructible()
{
static_assert( std::is_nothrow_copy_constructible<T>::value, "");
static_assert( std::is_nothrow_copy_constructible<const T>::value, "");
static_assert( std::is_nothrow_copy_constructible<volatile T>::value, "");
static_assert( std::is_nothrow_copy_constructible<const volatile T>::value, "");
}
template <class T>
@ -35,12 +33,6 @@ class Empty
{
};
class NotEmpty
{
public:
virtual ~NotEmpty();
};
union Union {};
struct bit_zero
@ -65,6 +57,5 @@ int main()
test_is_nothrow_copy_constructible<double>();
test_is_nothrow_copy_constructible<int*>();
test_is_nothrow_copy_constructible<const int*>();
test_is_nothrow_copy_constructible<NotEmpty>();
test_is_nothrow_copy_constructible<bit_zero>();
}

View File

@ -35,12 +35,6 @@ class Empty
{
};
class NotEmpty
{
public:
virtual ~NotEmpty();
};
union Union {};
struct bit_zero
@ -66,6 +60,5 @@ int main()
test_is_nothrow_default_constructible<int*>();
test_is_nothrow_default_constructible<const int*>();
test_is_nothrow_default_constructible<char[3]>();
test_is_nothrow_default_constructible<NotEmpty>();
test_is_nothrow_default_constructible<bit_zero>();
}

View File

@ -18,8 +18,6 @@ void test_is_nothrow_move_constructible()
{
static_assert( std::is_nothrow_move_constructible<T>::value, "");
static_assert( std::is_nothrow_move_constructible<const T>::value, "");
static_assert( std::is_nothrow_move_constructible<volatile T>::value, "");
static_assert( std::is_nothrow_move_constructible<const volatile T>::value, "");
}
template <class T>
@ -35,12 +33,6 @@ class Empty
{
};
class NotEmpty
{
public:
virtual ~NotEmpty();
};
union Union {};
struct bit_zero
@ -58,13 +50,12 @@ int main()
test_has_not_nothrow_move_constructor<void>();
test_has_not_nothrow_move_constructor<A>();
test_has_not_nothrow_move_constructor<int&>();
test_is_nothrow_move_constructible<int&>();
test_is_nothrow_move_constructible<Union>();
test_is_nothrow_move_constructible<Empty>();
test_is_nothrow_move_constructible<int>();
test_is_nothrow_move_constructible<double>();
test_is_nothrow_move_constructible<int*>();
test_is_nothrow_move_constructible<const int*>();
test_is_nothrow_move_constructible<NotEmpty>();
test_is_nothrow_move_constructible<bit_zero>();
}

View File

@ -13,9 +13,18 @@
#include <type_traits>
template <class T1, class T2>
struct pair
{
T1 first;
T2 second;
};
int main()
{
static_assert( std::is_standard_layout<int>::value, "");
static_assert( std::is_standard_layout<int[3]>::value, "");
static_assert(!std::is_standard_layout<int&>::value, "");
static_assert(!std::is_standard_layout<volatile int&>::value, "");
static_assert(( std::is_standard_layout<pair<int, double> >::value), "");
}

View File

@ -13,9 +13,19 @@
#include <type_traits>
struct A {};
class B
{
public:
B();
};
int main()
{
static_assert( std::is_trivial<int>::value, "");
static_assert(!std::is_trivial<int&>::value, "");
static_assert(!std::is_trivial<volatile int&>::value, "");
static_assert( std::is_trivial<A>::value, "");
static_assert(!std::is_trivial<B>::value, "");
}

View File

@ -25,6 +25,12 @@ struct B
~B() {assert(i_ == 0);}
};
class C
{
public:
C();
};
int main()
{
static_assert( std::is_trivially_copyable<int>::value, "");
@ -34,4 +40,5 @@ int main()
static_assert( std::is_trivially_copyable<const A>::value, "");
static_assert(!std::is_trivially_copyable<const A&>::value, "");
static_assert(!std::is_trivially_copyable<B>::value, "");
static_assert( std::is_trivially_copyable<C>::value, "");
}