1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -ffreestanding -Wno-null-conversion -Wno-tautological-compare %s
4 typedef decltype(nullptr) nullptr_t
;
10 void o2(char*); // expected-note {{candidate}}
11 void o2(int A::*); // expected-note {{candidate}}
13 nullptr_t
f(nullptr_t null
)
15 // Implicit conversions.
24 void (*pf
)() = nullptr;
26 void (A::*pmf
)() = nullptr;
28 bool b
= nullptr; // expected-error {{cannot initialize}}
30 // Can't convert nullptr to integral implicitly.
31 uintptr_t i
= nullptr; // expected-error {{cannot initialize}}
34 (void)(null
== nullptr);
35 (void)(null
<= nullptr); // expected-error {{invalid operands to binary expression}}
37 (void)(null
== (void*)0);
38 (void)((void*)0 == nullptr);
39 (void)(null
<= 0); // expected-error {{invalid operands to binary expression}}
40 (void)(null
<= (void*)0); // expected-error {{invalid operands to binary expression}}
41 (void)((void*)0 <= nullptr); // expected-error {{invalid operands to binary expression}}
44 (void)(nullptr <= 0); // expected-error {{invalid operands to binary expression}}
45 (void)(0 <= nullptr); // expected-error {{invalid operands to binary expression}}
46 (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}}
49 (void)(0 ? nullptr : 0);
50 (void)(0 ? nullptr : (void*)0);
51 (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}}
57 o2(nullptr); // expected-error {{ambiguous}}
59 // nullptr is an rvalue, null is an lvalue
60 (void)&nullptr; // expected-error {{cannot take the address of an rvalue of type 'std::nullptr_t'}}
61 nullptr_t
*pn
= &null
;
63 // You can reinterpret_cast nullptr to an integer.
64 (void)reinterpret_cast<uintptr_t>(nullptr);
65 (void)reinterpret_cast<uintptr_t>(*pn
);
67 // You can't reinterpret_cast nullptr to any integer
68 (void)reinterpret_cast<char>(nullptr); // expected-error {{cast from pointer to smaller type 'char' loses information}}
73 // You can throw nullptr.
77 // Template arguments can be nullptr.
78 template <int *PI
, void (*PF
)(), int A::*PM
, void (A::*PMF
)()>
81 typedef T
<nullptr, nullptr, nullptr, nullptr> NT
;
84 template<typename T
, typename U
> struct is_same
{
85 static const bool value
= false;
88 template<typename T
> struct is_same
<T
, T
> {
89 static const bool value
= true;
95 // Test that we prefer g(void*) over g(bool).
96 static_assert(is_same
<decltype(g(nullptr)), void*>::value
, "");
100 void f(int, ...) __attribute__((sentinel
));
103 // nullptr can be used as the sentinel value.
109 void f(const char*, ...) __attribute__((format(printf
, 1, 2)));
112 // Don't warn when using nullptr with %p.
117 static_assert(__is_scalar(nullptr_t
), "");
118 static_assert(__is_pod(nullptr_t
), "");
119 static_assert(sizeof(nullptr_t
) == sizeof(void*), "");
121 static_assert(!(nullptr < nullptr), ""); // expected-error {{invalid operands to binary expression}}
122 static_assert(!(nullptr > nullptr), ""); // expected-error {{invalid operands to binary expression}}
123 static_assert( nullptr <= nullptr, ""); // expected-error {{invalid operands to binary expression}}
124 static_assert( nullptr >= nullptr, ""); // expected-error {{invalid operands to binary expression}}
125 static_assert( nullptr == nullptr, "");
126 static_assert(!(nullptr != nullptr), "");
128 static_assert(!(0 < nullptr), ""); // expected-error {{invalid operands to binary expression}}
129 static_assert(!(0 > nullptr), ""); // expected-error {{invalid operands to binary expression}}
130 static_assert( 0 <= nullptr, ""); // expected-error {{invalid operands to binary expression}}
131 static_assert( 0 >= nullptr, ""); // expected-error {{invalid operands to binary expression}}
132 static_assert( 0 == nullptr, "");
133 static_assert(!(0 != nullptr), "");
135 static_assert(!(nullptr < 0), ""); // expected-error {{invalid operands to binary expression}}
136 static_assert(!(nullptr > 0), ""); // expected-error {{invalid operands to binary expression}}
137 static_assert( nullptr <= 0, ""); // expected-error {{invalid operands to binary expression}}
138 static_assert( nullptr >= 0, ""); // expected-error {{invalid operands to binary expression}}
139 static_assert( nullptr == 0, "");
140 static_assert(!(nullptr != 0), "");
142 namespace overloading
{
147 int &ir
= (f1
)(nullptr);
150 struct ConvertsToNullPtr
{
151 operator nullptr_t() const;
154 void test_conversion(ConvertsToNullPtr ctn
) {
157 (void)(ctn
<= ctn
); // expected-error {{invalid operands to binary expression}}
158 (void)(ctn
>= ctn
); // expected-error {{invalid operands to binary expression}}
159 (void)(ctn
< ctn
); // expected-error {{invalid operands to binary expression}}
160 (void)(ctn
> ctn
); // expected-error {{invalid operands to binary expression}}
164 namespace templates
{
165 template<typename T
, nullptr_t Value
>
175 template<int (*fp
)(int), int* p
, int A::* pmd
, int (A::*pmf
)(int)>
178 X2
<nullptr, nullptr, nullptr, nullptr> x2
;
181 namespace null_pointer_constant
{
183 // Pending implementation of core issue 903, ensure we don't allow any of the
184 // C++11 constant evaluation semantics in null pointer constants.
186 constexpr int null() { return 0; }
187 void *p
= S().n
; // expected-error {{cannot initialize}}
188 void *q
= null(); // expected-error {{cannot initialize}}