blob: b49f63b98051020e18ad4ac39fb8ddcc8ad425dd [file] [log] [blame]
Richard Smith762bb9d2011-10-13 22:29:44 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -ffreestanding %s
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002#include <stdint.h>
3
Anders Carlssonc8df0b62010-11-05 00:12:09 +00004typedef decltype(nullptr) nullptr_t;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00005
6struct A {};
7
8int o1(char*);
9void o1(uintptr_t);
10void o2(char*); // expected-note {{candidate}}
11void o2(int A::*); // expected-note {{candidate}}
12
13nullptr_t f(nullptr_t null)
14{
15 // Implicit conversions.
16 null = nullptr;
17 void *p = nullptr;
18 p = null;
19 int *pi = nullptr;
20 pi = null;
21 null = 0;
22 int A::*pm = nullptr;
23 pm = null;
24 void (*pf)() = nullptr;
25 pf = null;
26 void (A::*pmf)() = nullptr;
27 pmf = null;
28 bool b = nullptr;
29
30 // Can't convert nullptr to integral implicitly.
Eli Friedmancfdc81a2009-12-19 08:11:05 +000031 uintptr_t i = nullptr; // expected-error {{cannot initialize}}
Sebastian Redl6e8ed162009-05-10 18:38:11 +000032
33 // Operators
34 (void)(null == nullptr);
35 (void)(null <= nullptr);
Richard Smith50d61c82012-08-08 06:13:49 +000036 (void)(null == 0);
Sebastian Redl6e8ed162009-05-10 18:38:11 +000037 (void)(null == (void*)0);
38 (void)((void*)0 == nullptr);
Richard Smith50d61c82012-08-08 06:13:49 +000039 (void)(null <= 0);
Sebastian Redl6e8ed162009-05-10 18:38:11 +000040 (void)(null <= (void*)0);
41 (void)((void*)0 <= nullptr);
Anders Carlsson0c8209e2010-11-04 03:17:43 +000042 (void)(0 == nullptr);
43 (void)(nullptr == 0);
44 (void)(nullptr <= 0);
45 (void)(0 <= nullptr);
Sebastian Redl6e8ed162009-05-10 18:38:11 +000046 (void)(1 > nullptr); // expected-error {{invalid operands to binary expression}}
47 (void)(1 != nullptr); // expected-error {{invalid operands to binary expression}}
48 (void)(1 + nullptr); // expected-error {{invalid operands to binary expression}}
Richard Smith50d61c82012-08-08 06:13:49 +000049 (void)(0 ? nullptr : 0);
Sebastian Redl6e8ed162009-05-10 18:38:11 +000050 (void)(0 ? nullptr : (void*)0);
Chandler Carruth82214a82011-02-18 23:54:50 +000051 (void)(0 ? nullptr : A()); // expected-error {{non-pointer operand type 'A' incompatible with nullptr}}
52 (void)(0 ? A() : nullptr); // expected-error {{non-pointer operand type 'A' incompatible with nullptr}}
Sebastian Redl6e8ed162009-05-10 18:38:11 +000053
54 // Overloading
55 int t = o1(nullptr);
56 t = o1(null);
57 o2(nullptr); // expected-error {{ambiguous}}
58
59 // nullptr is an rvalue, null is an lvalue
Richard Smith3fa3fea2013-02-02 02:14:45 +000060 (void)&nullptr; // expected-error {{cannot take the address of an rvalue of type 'nullptr_t'}}
Sebastian Redl6e8ed162009-05-10 18:38:11 +000061 nullptr_t *pn = &null;
62
63 // You can reinterpret_cast nullptr to an integer.
64 (void)reinterpret_cast<uintptr_t>(nullptr);
Douglas Gregor84ee2ee2011-05-21 23:15:46 +000065 (void)reinterpret_cast<uintptr_t>(*pn);
66
67 int *ip = *pn;
68 if (*pn) { }
Sebastian Redl6e8ed162009-05-10 18:38:11 +000069
70 // You can throw nullptr.
71 throw nullptr;
72}
73
74// Template arguments can be nullptr.
75template <int *PI, void (*PF)(), int A::*PM, void (A::*PMF)()>
76struct T {};
77
78typedef T<nullptr, nullptr, nullptr, nullptr> NT;
Anders Carlssonc8df0b62010-11-05 00:12:09 +000079
80namespace test1 {
81template<typename T, typename U> struct is_same {
82 static const bool value = false;
83};
84
85template<typename T> struct is_same<T, T> {
86 static const bool value = true;
87};
88
89void *g(void*);
90bool g(bool);
91
92// Test that we prefer g(void*) over g(bool).
93static_assert(is_same<decltype(g(nullptr)), void*>::value, "");
94}
Anders Carlsson343e6ff2010-11-05 15:21:33 +000095
96namespace test2 {
97 void f(int, ...) __attribute__((sentinel));
98
99 void g() {
100 // nullptr can be used as the sentinel value.
101 f(10, nullptr);
102 }
103}
Anders Carlsson62425992010-11-06 14:58:53 +0000104
105namespace test3 {
106 void f(const char*, ...) __attribute__((format(printf, 1, 2)));
107
108 void g() {
109 // Don't warn when using nullptr with %p.
110 f("%p", nullptr);
111 }
112}
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000113
Dmitri Gribenkod5f55dc2012-02-15 13:30:53 +0000114static_assert(__is_scalar(nullptr_t), "");
115static_assert(__is_pod(nullptr_t), "");
116static_assert(sizeof(nullptr_t) == sizeof(void*), "");
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000117
Dmitri Gribenkod5f55dc2012-02-15 13:30:53 +0000118static_assert(!(nullptr < nullptr), "");
119static_assert(!(nullptr > nullptr), "");
120static_assert( nullptr <= nullptr, "");
121static_assert( nullptr >= nullptr, "");
122static_assert( nullptr == nullptr, "");
123static_assert(!(nullptr != nullptr), "");
Richard Smith26f2cac2012-02-14 22:35:28 +0000124
Dmitri Gribenkod5f55dc2012-02-15 13:30:53 +0000125static_assert(!(0 < nullptr), "");
126static_assert(!(0 > nullptr), "");
127static_assert( 0 <= nullptr, "");
128static_assert( 0 >= nullptr, "");
129static_assert( 0 == nullptr, "");
130static_assert(!(0 != nullptr), "");
Richard Smith26f2cac2012-02-14 22:35:28 +0000131
Dmitri Gribenkod5f55dc2012-02-15 13:30:53 +0000132static_assert(!(nullptr < 0), "");
133static_assert(!(nullptr > 0), "");
134static_assert( nullptr <= 0, "");
135static_assert( nullptr >= 0, "");
136static_assert( nullptr == 0, "");
137static_assert(!(nullptr != 0), "");
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000138
139namespace overloading {
140 int &f1(int*);
141 float &f1(bool);
142
143 void test_f1() {
144 int &ir = (f1)(nullptr);
145 }
146
147 struct ConvertsToNullPtr {
148 operator nullptr_t() const;
149 };
150
151 void test_conversion(ConvertsToNullPtr ctn) {
152 (void)(ctn == ctn);
153 (void)(ctn != ctn);
154 (void)(ctn <= ctn);
155 (void)(ctn >= ctn);
156 (void)(ctn < ctn);
157 (void)(ctn > ctn);
158 }
159}
160
161namespace templates {
162 template<typename T, nullptr_t Value>
163 struct X {
164 X() { ptr = Value; }
165
166 T *ptr;
167 };
168
169 X<int, nullptr> x;
170
171
172 template<int (*fp)(int), int* p, int A::* pmd, int (A::*pmf)(int)>
173 struct X2 {};
174
175 X2<nullptr, nullptr, nullptr, nullptr> x2;
176}
Richard Smith70488e22012-02-14 21:38:30 +0000177
178namespace null_pointer_constant {
179
180// Pending implementation of core issue 903, ensure we don't allow any of the
181// C++11 constant evaluation semantics in null pointer constants.
182struct S { int n; };
183constexpr int null() { return 0; }
184void *p = S().n; // expected-error {{cannot initialize}}
185void *q = null(); // expected-error {{cannot initialize}}
186
187}