blob: 0a0d098e364f6a187bf7d94a5f3414f329980eb3 [file] [log] [blame]
Daniel Dunbarc79f7672010-09-07 22:54:28 +00001// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -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);
36 (void)(null == (void*)0);
37 (void)((void*)0 == nullptr);
38 (void)(null <= (void*)0);
39 (void)((void*)0 <= nullptr);
Anders Carlsson0c8209e2010-11-04 03:17:43 +000040 (void)(0 == nullptr);
41 (void)(nullptr == 0);
42 (void)(nullptr <= 0);
43 (void)(0 <= nullptr);
Sebastian Redl6e8ed162009-05-10 18:38:11 +000044 (void)(1 > nullptr); // expected-error {{invalid operands to binary expression}}
45 (void)(1 != nullptr); // expected-error {{invalid operands to binary expression}}
46 (void)(1 + nullptr); // expected-error {{invalid operands to binary expression}}
47 (void)(0 ? nullptr : 0); // expected-error {{incompatible operand types}}
48 (void)(0 ? nullptr : (void*)0);
49
50 // Overloading
51 int t = o1(nullptr);
52 t = o1(null);
53 o2(nullptr); // expected-error {{ambiguous}}
54
55 // nullptr is an rvalue, null is an lvalue
56 (void)&nullptr; // expected-error {{address expression must be an lvalue}}
57 nullptr_t *pn = &null;
58
59 // You can reinterpret_cast nullptr to an integer.
60 (void)reinterpret_cast<uintptr_t>(nullptr);
61
62 // You can throw nullptr.
63 throw nullptr;
64}
65
66// Template arguments can be nullptr.
67template <int *PI, void (*PF)(), int A::*PM, void (A::*PMF)()>
68struct T {};
69
70typedef T<nullptr, nullptr, nullptr, nullptr> NT;
Anders Carlssonc8df0b62010-11-05 00:12:09 +000071
72namespace test1 {
73template<typename T, typename U> struct is_same {
74 static const bool value = false;
75};
76
77template<typename T> struct is_same<T, T> {
78 static const bool value = true;
79};
80
81void *g(void*);
82bool g(bool);
83
84// Test that we prefer g(void*) over g(bool).
85static_assert(is_same<decltype(g(nullptr)), void*>::value, "");
86}