1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2f4a2713aSLionel Sambuc
3f4a2713aSLionel Sambuc template<typename T>
4f4a2713aSLionel Sambuc class unique_ptr {
5f4a2713aSLionel Sambuc T *ptr;
6f4a2713aSLionel Sambuc
7*0a6a1f1dSLionel Sambuc unique_ptr(const unique_ptr&) = delete; // expected-note 3{{'unique_ptr' has been explicitly marked deleted here}}
8f4a2713aSLionel Sambuc unique_ptr &operator=(const unique_ptr&) = delete; // expected-note{{candidate function has been explicitly deleted}}
9f4a2713aSLionel Sambuc public:
unique_ptr()10f4a2713aSLionel Sambuc unique_ptr() : ptr(0) { }
unique_ptr(unique_ptr && other)11f4a2713aSLionel Sambuc unique_ptr(unique_ptr &&other) : ptr(other.ptr) { other.ptr = 0; }
unique_ptr(T * ptr)12f4a2713aSLionel Sambuc explicit unique_ptr(T *ptr) : ptr(ptr) { }
13f4a2713aSLionel Sambuc
~unique_ptr()14f4a2713aSLionel Sambuc ~unique_ptr() { delete ptr; }
15f4a2713aSLionel Sambuc
operator =(unique_ptr && other)16f4a2713aSLionel Sambuc unique_ptr &operator=(unique_ptr &&other) { // expected-note{{candidate function not viable: no known conversion from 'unique_ptr<int>' to 'unique_ptr<int> &&' for 1st argument}}
17f4a2713aSLionel Sambuc if (this == &other)
18f4a2713aSLionel Sambuc return *this;
19f4a2713aSLionel Sambuc
20f4a2713aSLionel Sambuc delete ptr;
21f4a2713aSLionel Sambuc ptr = other.ptr;
22f4a2713aSLionel Sambuc other.ptr = 0;
23f4a2713aSLionel Sambuc return *this;
24f4a2713aSLionel Sambuc }
25f4a2713aSLionel Sambuc };
26f4a2713aSLionel Sambuc
27f4a2713aSLionel Sambuc template<typename T>
28f4a2713aSLionel Sambuc struct remove_reference {
29f4a2713aSLionel Sambuc typedef T type;
30f4a2713aSLionel Sambuc };
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc template<typename T>
33f4a2713aSLionel Sambuc struct remove_reference<T&> {
34f4a2713aSLionel Sambuc typedef T type;
35f4a2713aSLionel Sambuc };
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc template<typename T>
38f4a2713aSLionel Sambuc struct remove_reference<T&&> {
39f4a2713aSLionel Sambuc typedef T type;
40f4a2713aSLionel Sambuc };
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc
move(T && t)43f4a2713aSLionel Sambuc template <class T> typename remove_reference<T>::type&& move(T&& t) {
44f4a2713aSLionel Sambuc return static_cast<typename remove_reference<T>::type&&>(t);
45f4a2713aSLionel Sambuc }
46f4a2713aSLionel Sambuc
forward(typename remove_reference<T>::type & t)47f4a2713aSLionel Sambuc template <class T> T&& forward(typename remove_reference<T>::type& t) {
48f4a2713aSLionel Sambuc return static_cast<T&&>(t);
49f4a2713aSLionel Sambuc }
50f4a2713aSLionel Sambuc
forward(typename remove_reference<T>::type && t)51f4a2713aSLionel Sambuc template <class T> T&& forward(typename remove_reference<T>::type&& t) {
52f4a2713aSLionel Sambuc return static_cast<T&&>(t);
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc template<typename T, typename ...Args>
make_unique_ptr(Args &&...args)56f4a2713aSLionel Sambuc unique_ptr<T> make_unique_ptr(Args &&...args) {
57f4a2713aSLionel Sambuc return unique_ptr<T>(new T(forward<Args>(args)...));
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc template<typename T> void accept_unique_ptr(unique_ptr<T>); // expected-note{{passing argument to parameter here}}
61f4a2713aSLionel Sambuc
test_unique_ptr()62f4a2713aSLionel Sambuc unique_ptr<int> test_unique_ptr() {
63f4a2713aSLionel Sambuc // Simple construction
64f4a2713aSLionel Sambuc unique_ptr<int> p;
65f4a2713aSLionel Sambuc unique_ptr<int> p1(new int);
66f4a2713aSLionel Sambuc
67f4a2713aSLionel Sambuc // Move construction
68f4a2713aSLionel Sambuc unique_ptr<int> p2(make_unique_ptr<int>(17));
69f4a2713aSLionel Sambuc unique_ptr<int> p3 = make_unique_ptr<int>(17);
70f4a2713aSLionel Sambuc
71f4a2713aSLionel Sambuc // Copy construction (failures)
72f4a2713aSLionel Sambuc unique_ptr<int> p4(p); // expected-error{{call to deleted constructor of 'unique_ptr<int>'}}
73f4a2713aSLionel Sambuc unique_ptr<int> p5 = p; // expected-error{{call to deleted constructor of 'unique_ptr<int>'}}
74f4a2713aSLionel Sambuc
75f4a2713aSLionel Sambuc // Move assignment
76f4a2713aSLionel Sambuc p2 = move(p);
77f4a2713aSLionel Sambuc p2 = make_unique_ptr<int>(0);
78f4a2713aSLionel Sambuc
79f4a2713aSLionel Sambuc // Copy assignment (failures);
80f4a2713aSLionel Sambuc p2 = p3; // expected-error{{overload resolution selected deleted operator '='}}
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambuc // Implicit copies
83f4a2713aSLionel Sambuc accept_unique_ptr(make_unique_ptr<double>(0.0));
84f4a2713aSLionel Sambuc accept_unique_ptr(move(p2));
85f4a2713aSLionel Sambuc
86f4a2713aSLionel Sambuc // Implicit copies (failures);
87f4a2713aSLionel Sambuc accept_unique_ptr(p); // expected-error{{call to deleted constructor of 'unique_ptr<int>'}}
88f4a2713aSLionel Sambuc
89f4a2713aSLionel Sambuc return p;
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc namespace perfect_forwarding {
93f4a2713aSLionel Sambuc struct A { };
94f4a2713aSLionel Sambuc
95f4a2713aSLionel Sambuc struct F0 {
96f4a2713aSLionel Sambuc void operator()(A&, const A&, A&&, const A&&, A&&, const A&&); // expected-note{{candidate function not viable: 5th argument ('const perfect_forwarding::A') would lose const qualifier}}
97f4a2713aSLionel Sambuc };
98f4a2713aSLionel Sambuc
99f4a2713aSLionel Sambuc template<typename F, typename ...Args>
forward(F f,Args &&...args)100f4a2713aSLionel Sambuc void forward(F f, Args &&...args) {
101f4a2713aSLionel Sambuc f(static_cast<Args&&>(args)...); // expected-error{{no matching function for call to object of type 'perfect_forwarding::F0'}}
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc template<typename T> T get();
105f4a2713aSLionel Sambuc
test_forward()106f4a2713aSLionel Sambuc void test_forward() {
107f4a2713aSLionel Sambuc forward(F0(), get<A&>(), get<A const&>(), get<A>(), get<const A>(),
108f4a2713aSLionel Sambuc get<A&&>(), get<const A&&>());
109f4a2713aSLionel Sambuc forward(F0(), get<A&>(), get<A const&>(), get<A>(), get<const A>(), // expected-note{{in instantiation of function template specialization 'perfect_forwarding::forward<perfect_forwarding::F0, perfect_forwarding::A &, const perfect_forwarding::A &, perfect_forwarding::A, const perfect_forwarding::A, const perfect_forwarding::A, const perfect_forwarding::A>' requested here}}
110f4a2713aSLionel Sambuc get<const A&&>(), get<const A&&>());
111f4a2713aSLionel Sambuc }
112f4a2713aSLionel Sambuc };
113