]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/language.support/support.types/nullptr_t.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / std / language.support / support.types / nullptr_t.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include <cstddef>
11 #include <type_traits>
12 #include <cassert>
13
14 #include "test_macros.h"
15
16 // typedef decltype(nullptr) nullptr_t;
17
18 struct A
19 {
20     A(std::nullptr_t) {}
21 };
22
23 template <class T>
24 void test_conversions()
25 {
26     {
27         T p = 0;
28         assert(p == nullptr);
29     }
30     {
31         T p = nullptr;
32         assert(p == nullptr);
33         assert(nullptr == p);
34         assert(!(p != nullptr));
35         assert(!(nullptr != p));
36     }
37 }
38
39 template <class T> struct Voider { typedef void type; };
40 template <class T, class = void> struct has_less : std::false_type {};
41
42 template <class T> struct has_less<T,
43     typename Voider<decltype(std::declval<T>() < nullptr)>::type> : std::true_type {};
44
45 template <class T>
46 void test_comparisons()
47 {
48     T p = nullptr;
49     assert(p == nullptr);
50     assert(!(p != nullptr));
51     assert(nullptr == p);
52     assert(!(nullptr != p));
53 }
54
55 #if defined(__clang__)
56 #pragma clang diagnostic push
57 #pragma clang diagnostic ignored "-Wnull-conversion"
58 #endif
59 void test_nullptr_conversions() {
60 // GCC does not accept this due to CWG Defect #1423
61 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1423
62 #if defined(__clang__)
63     {
64         bool b = nullptr;
65         assert(!b);
66     }
67 #endif
68     {
69         bool b(nullptr);
70         assert(!b);
71     }
72 }
73 #if defined(__clang__)
74 #pragma clang diagnostic pop
75 #endif
76
77
78 int main()
79 {
80     static_assert(sizeof(std::nullptr_t) == sizeof(void*),
81                   "sizeof(std::nullptr_t) == sizeof(void*)");
82
83     {
84         test_conversions<std::nullptr_t>();
85         test_conversions<void*>();
86         test_conversions<A*>();
87         test_conversions<void(*)()>();
88         test_conversions<void(A::*)()>();
89         test_conversions<int A::*>();
90     }
91     {
92 #ifdef _LIBCPP_HAS_NO_NULLPTR
93         static_assert(!has_less<std::nullptr_t>::value, "");
94         // FIXME: our c++03 nullptr emulation still allows for comparisons
95         // with other pointer types by way of the conversion operator.
96         //static_assert(!has_less<void*>::value, "");
97 #else
98         // TODO Enable this assertion when all compilers implement core DR 583.
99         // static_assert(!has_less<std::nullptr_t>::value, "");
100 #endif
101         test_comparisons<std::nullptr_t>();
102         test_comparisons<void*>();
103         test_comparisons<A*>();
104         test_comparisons<void(*)()>();
105     }
106     test_nullptr_conversions();
107 }