Skip to content

Commit 467fc38

Browse files
author
Howard Hinnant
committed
A much improved type_traits for C++0x. Not yet done: is_trivially_constructible, is_trivially_assignable and underlying_type.
llvm-svn: 131291
1 parent 5dbf45d commit 467fc38

18 files changed

+156
-55
lines changed

libcxx/include/type_traits

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ namespace std
129129
template <class T> struct alignment_of;
130130
template <size_t Len, size_t Align = most_stringent_alignment_requirement>
131131
struct aligned_storage;
132-
template <std::size_t Len, class... Types> struct aligned_union;
133132
134133
template <class T> struct decay;
135134
template <class... T> struct common_type;
@@ -169,11 +168,15 @@ struct _LIBCPP_VISIBLE integral_constant
169168
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
170169
constexpr
171170
#endif
172-
operator value_type() const {return value;}
171+
operator value_type()
172+
#ifdef _LIBCPP_HAS_NO_CONSTEXPR
173+
const
174+
#endif
175+
{return value;}
173176
};
174177

175178
template <class _Tp, _Tp __v>
176-
const _Tp integral_constant<_Tp, __v>::value;
179+
constexpr _Tp integral_constant<_Tp, __v>::value;
177180

178181
typedef integral_constant<bool, true> true_type;
179182
typedef integral_constant<bool, false> false_type;
@@ -1200,7 +1203,7 @@ decltype((_STD::declval<_Tp>() = _STD::declval<_Arg>(), true_type()))
12001203
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
12011204
__is_assignable_test(_Tp&&, _Arg&&);
12021205
#else
1203-
__is_assignable_test(_Tp&, _Arg&);
1206+
__is_assignable_test(_Tp, _Arg&);
12041207
#endif
12051208

12061209
template <class _Arg>
@@ -1211,27 +1214,35 @@ __is_assignable_test(__any, _Arg&&);
12111214
__is_assignable_test(__any, _Arg&);
12121215
#endif
12131216

1214-
template <class _Tp, class _Arg>
1217+
template <class _Tp, class _Arg, bool = is_void<_Tp>::value || is_void<_Arg>::value>
12151218
struct __is_assignable_imp
12161219
: public common_type
12171220
<
12181221
decltype(__is_assignable_test(declval<_Tp>(), declval<_Arg>()))
12191222
>::type {};
12201223

1224+
template <class _Tp, class _Arg>
1225+
struct __is_assignable_imp<_Tp, _Arg, true>
1226+
: public false_type
1227+
{
1228+
};
1229+
12211230
template <class _Tp, class _Arg>
12221231
struct is_assignable
12231232
: public __is_assignable_imp<_Tp, _Arg> {};
12241233

12251234
// is_copy_assignable
12261235

12271236
template <class _Tp> struct _LIBCPP_VISIBLE is_copy_assignable
1228-
: public is_assignable<_Tp&, const _Tp&> {};
1237+
: public is_assignable<typename add_lvalue_reference<_Tp>::type,
1238+
const typename add_lvalue_reference<_Tp>::type> {};
12291239

12301240
// is_move_assignable
12311241

12321242
template <class _Tp> struct _LIBCPP_VISIBLE is_move_assignable
12331243
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1234-
: public is_assignable<_Tp&, _Tp&&> {};
1244+
: public is_assignable<typename add_lvalue_reference<_Tp>::type,
1245+
const typename add_rvalue_reference<_Tp>::type> {};
12351246
#else
12361247
: public is_copy_assignable<_Tp> {};
12371248
#endif
@@ -2143,6 +2154,12 @@ struct _LIBCPP_VISIBLE is_nothrow_constructible
21432154
{
21442155
};
21452156

2157+
template <class _Tp, size_t _Ns>
2158+
struct _LIBCPP_VISIBLE is_nothrow_constructible<_Tp[_Ns]>
2159+
: __is_nothrow_constructible<is_constructible<_Tp>::value, _Tp>
2160+
{
2161+
};
2162+
21462163
#else // __has_feature(cxx_noexcept)
21472164

21482165
template <class _Tp, class... _Args>
@@ -2384,6 +2401,28 @@ struct _LIBCPP_VISIBLE is_nothrow_destructible
23842401
{
23852402
};
23862403

2404+
template <class _Tp, size_t _Ns>
2405+
struct _LIBCPP_VISIBLE is_nothrow_destructible<_Tp[_Ns]>
2406+
: public is_nothrow_destructible<_Tp>
2407+
{
2408+
};
2409+
2410+
template <class _Tp>
2411+
struct _LIBCPP_VISIBLE is_nothrow_destructible<_Tp&>
2412+
: public true_type
2413+
{
2414+
};
2415+
2416+
#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
2417+
2418+
template <class _Tp>
2419+
struct _LIBCPP_VISIBLE is_nothrow_destructible<_Tp&&>
2420+
: public true_type
2421+
{
2422+
};
2423+
2424+
#endif
2425+
23872426
#else
23882427

23892428
template <class _Tp> struct __libcpp_nothrow_destructor
@@ -2423,6 +2462,37 @@ template <class _Tp> struct _LIBCPP_VISIBLE is_literal_type
24232462
#endif
24242463
{};
24252464

2465+
// is_standard_layout;
2466+
2467+
template <class _Tp> struct _LIBCPP_VISIBLE is_standard_layout
2468+
#if __has_feature(is_standard_layout)
2469+
: public integral_constant<bool, __is_standard_layout(_Tp)>
2470+
#else
2471+
: integral_constant<bool, is_scalar<typename remove_all_extents<_Tp>::type>::value>
2472+
#endif
2473+
{};
2474+
2475+
// is_trivially_copyable;
2476+
2477+
template <class _Tp> struct _LIBCPP_VISIBLE is_trivially_copyable
2478+
#if __has_feature(is_trivially_copyable)
2479+
: public integral_constant<bool, __is_trivially_copyable(_Tp)>
2480+
#else
2481+
: integral_constant<bool, is_scalar<typename remove_all_extents<_Tp>::type>::value>
2482+
#endif
2483+
{};
2484+
2485+
// is_trivial;
2486+
2487+
template <class _Tp> struct _LIBCPP_VISIBLE is_trivial
2488+
#if __has_feature(is_trivial)
2489+
: public integral_constant<bool, __is_trivial(_Tp)>
2490+
#else
2491+
: integral_constant<bool, is_trivially_copyable<_Tp>::value &&
2492+
is_trivially_default_constructible<T>::value>::value>
2493+
#endif
2494+
{};
2495+
24262496
template <class _Tp>
24272497
inline _LIBCPP_INLINE_VISIBILITY
24282498
void

libcxx/test/utilities/meta/meta.hel/integral_constant.pass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ int main()
2020
static_assert(_5::value == 5, "");
2121
static_assert((std::is_same<_5::value_type, int>::value), "");
2222
static_assert((std::is_same<_5::type, _5>::value), "");
23+
#ifndef _LIBCPP_HAS_NO_CONSTEXPR
2324
static_assert((_5() == 5), "");
25+
#else
26+
assert(_5() == 5);
27+
#endif
2428

2529
static_assert(std::false_type::value == false, "");
2630
static_assert((std::is_same<std::false_type::value_type, bool>::value), "");

libcxx/test/utilities/meta/meta.trans/meta.trans.other/aligned_union.pass.cpp

Lines changed: 0 additions & 19 deletions
This file was deleted.

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_assignable.pass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,8 @@ int main()
3131
static_assert(( std::is_assignable<int&, double>::value), "");
3232
static_assert(( std::is_assignable<B, A>::value), "");
3333
static_assert((!std::is_assignable<A, B>::value), "");
34+
static_assert((!std::is_assignable<void, const void>::value), "");
35+
static_assert((!std::is_assignable<const void, const void>::value), "");
36+
static_assert(( std::is_assignable<void*&, void*>::value), "");
37+
static_assert((!std::is_assignable<int(), int>::value), "");
3438
}

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_constructible.pass.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ struct A
1818
{
1919
explicit A(int);
2020
A(int, double);
21+
private:
22+
A(char);
2123
};
2224

2325
int main()
@@ -27,4 +29,9 @@ int main()
2729
static_assert((std::is_constructible<A, int>::value), "");
2830
static_assert((std::is_constructible<A, int, double>::value), "");
2931
static_assert((!std::is_constructible<A>::value), "");
32+
static_assert((!std::is_constructible<A, char>::value), "");
33+
static_assert((!std::is_constructible<A, void>::value), "");
34+
static_assert((!std::is_constructible<void>::value), "");
35+
static_assert((!std::is_constructible<int&>::value), "");
36+
static_assert(( std::is_constructible<int&, int&>::value), "");
3037
}

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_copy_assignable.pass.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ struct A
3535
A();
3636
};
3737

38+
class B
39+
{
40+
B& operator=(const B&);
41+
};
42+
3843
int main()
3944
{
4045
static_assert(( std::is_copy_assignable<int>::value), "");
@@ -47,4 +52,6 @@ int main()
4752
static_assert(( std::is_copy_assignable<Union>::value), "");
4853
static_assert(( std::is_copy_assignable<NotEmpty>::value), "");
4954
static_assert(( std::is_copy_assignable<Empty>::value), "");
55+
static_assert((!std::is_copy_assignable<B>::value), "");
56+
static_assert((!std::is_copy_assignable<void>::value), "");
5057
}

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_copy_constructible.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ struct A
4747
A(const A&);
4848
};
4949

50+
class B
51+
{
52+
B(const B&);
53+
};
54+
5055
int main()
5156
{
5257
test_is_copy_constructible<char[3], false>();
@@ -55,6 +60,7 @@ int main()
5560
test_is_copy_constructible<Abstract, false>();
5661

5762
test_is_copy_constructible<A, true>();
63+
test_is_copy_constructible<B, false>();
5864
test_is_copy_constructible<int&, true>();
5965
test_is_copy_constructible<Union, true>();
6066
test_is_copy_constructible<Empty, true>();

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_default_constructible.pass.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ struct A
5050
A();
5151
};
5252

53+
class B
54+
{
55+
B();
56+
};
57+
5358
int main()
5459
{
5560
test_is_default_constructible<void, false>();
@@ -58,6 +63,7 @@ int main()
5863
test_is_default_constructible<Abstract, false>();
5964

6065
test_is_default_constructible<A, true>();
66+
test_is_default_constructible<B, false>();
6167
test_is_default_constructible<Union, true>();
6268
test_is_default_constructible<Empty, true>();
6369
test_is_default_constructible<int, true>();

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_literal_type.pass.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@
1313

1414
#include <type_traits>
1515

16+
struct A
17+
{
18+
};
19+
20+
struct B
21+
{
22+
B();
23+
};
24+
1625
int main()
1726
{
1827
static_assert( std::is_literal_type<int>::value, "");
1928
static_assert( std::is_literal_type<const int>::value, "");
20-
static_assert(!std::is_literal_type<int&>::value, "");
21-
static_assert(!std::is_literal_type<volatile int&>::value, "");
29+
static_assert( std::is_literal_type<int&>::value, "");
30+
static_assert( std::is_literal_type<volatile int&>::value, "");
31+
static_assert( std::is_literal_type<A>::value, "");
32+
static_assert(!std::is_literal_type<B>::value, "");
2233
}

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_move_assignable.pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ int main()
4141
static_assert((!std::is_move_assignable<const int>::value), "");
4242
static_assert((!std::is_move_assignable<int[]>::value), "");
4343
static_assert((!std::is_move_assignable<int[3]>::value), "");
44-
static_assert(( std::is_move_assignable<int&>::value), "");
44+
static_assert((!std::is_move_assignable<int[3]>::value), "");
45+
static_assert((!std::is_move_assignable<void>::value), "");
4546
static_assert(( std::is_move_assignable<A>::value), "");
4647
static_assert(( std::is_move_assignable<bit_zero>::value), "");
4748
static_assert(( std::is_move_assignable<Union>::value), "");

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_constructible.pass.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ int main()
4747
static_assert((!std::is_nothrow_constructible<A, int>::value), "");
4848
static_assert((!std::is_nothrow_constructible<A, int, double>::value), "");
4949
static_assert((!std::is_nothrow_constructible<A>::value), "");
50+
static_assert(( std::is_nothrow_constructible<Empty>::value), "");
51+
static_assert(( std::is_nothrow_constructible<Empty, const Empty&>::value), "");
5052
}

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_copy_assignable.pass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct A
4242

4343
int main()
4444
{
45+
test_has_nothrow_assign<const int, false>();
4546
test_has_nothrow_assign<void, false>();
4647
test_has_nothrow_assign<A, false>();
4748
test_has_nothrow_assign<int&, true>();

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_copy_constructible.pass.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ void test_is_nothrow_copy_constructible()
1818
{
1919
static_assert( std::is_nothrow_copy_constructible<T>::value, "");
2020
static_assert( std::is_nothrow_copy_constructible<const T>::value, "");
21-
static_assert( std::is_nothrow_copy_constructible<volatile T>::value, "");
22-
static_assert( std::is_nothrow_copy_constructible<const volatile T>::value, "");
2321
}
2422

2523
template <class T>
@@ -35,12 +33,6 @@ class Empty
3533
{
3634
};
3735

38-
class NotEmpty
39-
{
40-
public:
41-
virtual ~NotEmpty();
42-
};
43-
4436
union Union {};
4537

4638
struct bit_zero
@@ -65,6 +57,5 @@ int main()
6557
test_is_nothrow_copy_constructible<double>();
6658
test_is_nothrow_copy_constructible<int*>();
6759
test_is_nothrow_copy_constructible<const int*>();
68-
test_is_nothrow_copy_constructible<NotEmpty>();
6960
test_is_nothrow_copy_constructible<bit_zero>();
7061
}

libcxx/test/utilities/meta/meta.unary/meta.unary.prop/is_nothrow_default_constructible.pass.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ class Empty
3535
{
3636
};
3737

38-
class NotEmpty
39-
{
40-
public:
41-
virtual ~NotEmpty();
42-
};
43-
4438
union Union {};
4539

4640
struct bit_zero
@@ -66,6 +60,5 @@ int main()
6660
test_is_nothrow_default_constructible<int*>();
6761
test_is_nothrow_default_constructible<const int*>();
6862
test_is_nothrow_default_constructible<char[3]>();
69-
test_is_nothrow_default_constructible<NotEmpty>();
7063
test_is_nothrow_default_constructible<bit_zero>();
7164
}

0 commit comments

Comments
 (0)