Home | History | Annotate | Download | only in forward
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // test move
     11 
     12 #include <utility>
     13 #include <cassert>
     14 
     15 int copy_ctor = 0;
     16 int move_ctor = 0;
     17 
     18 class A
     19 {
     20 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
     21 #else
     22 #endif
     23 
     24 public:
     25 
     26 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
     27     A(const A&) {++copy_ctor;}
     28     A& operator=(const A&);
     29 
     30     A(A&&) {++move_ctor;}
     31     A& operator=(A&&);
     32 #else  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
     33     A(const A&) {++copy_ctor;}
     34     A& operator=(A&);
     35 
     36     operator std::__rv<A> () {return std::__rv<A>(*this);}
     37     A(std::__rv<A>) {++move_ctor;}
     38 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
     39 
     40     A() {}
     41 };
     42 
     43 A source() {return A();}
     44 const A csource() {return A();}
     45 
     46 void test(A) {}
     47 
     48 int main()
     49 {
     50     A a;
     51     const A ca = A();
     52 
     53     assert(copy_ctor == 0);
     54     assert(move_ctor == 0);
     55 
     56     A a2 = a;
     57     assert(copy_ctor == 1);
     58     assert(move_ctor == 0);
     59 
     60     A a3 = std::move(a);
     61     assert(copy_ctor == 1);
     62     assert(move_ctor == 1);
     63 
     64     A a4 = ca;
     65     assert(copy_ctor == 2);
     66     assert(move_ctor == 1);
     67 
     68     A a5 = std::move(ca);
     69     assert(copy_ctor == 3);
     70     assert(move_ctor == 1);
     71 }
     72