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