From fcc94dd57ce22044302a5eced3de3e4dcdd51c15 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Fri, 21 Jan 2011 17:06:29 +0000 Subject: [PATCH] Add unique_ptr example to test the use of rvalue references. I'll grow this example further as more rvalue-reference features come online. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@123980 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/SemaCXX/rval-references-examples.cpp | 88 +++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 test/SemaCXX/rval-references-examples.cpp diff --git a/test/SemaCXX/rval-references-examples.cpp b/test/SemaCXX/rval-references-examples.cpp new file mode 100644 index 000000000..1cde5854b --- /dev/null +++ b/test/SemaCXX/rval-references-examples.cpp @@ -0,0 +1,88 @@ +// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s + +template +class unique_ptr { + T *ptr; + + unique_ptr(const unique_ptr&) = delete; // expected-note 3{{function has been explicitly marked deleted here}} + unique_ptr &operator=(const unique_ptr&) = delete; // expected-note{{candidate function has been explicitly deleted}} +public: + unique_ptr() : ptr(0) { } + unique_ptr(unique_ptr &&other) : ptr(other.ptr) { other.ptr = 0; } + explicit unique_ptr(T *ptr) : ptr(ptr) { } + + ~unique_ptr() { delete ptr; } + + unique_ptr &operator=(unique_ptr &&other) { // expected-note{{candidate function not viable: no known conversion from 'unique_ptr' to 'unique_ptr &&' for 1st argument}} + if (this == &other) + return *this; + + delete ptr; + ptr = other.ptr; + other.ptr = 0; + return *this; + } +}; + +template +struct remove_reference { + typedef T type; +}; + +template +struct remove_reference { + typedef T type; +}; + +template +struct remove_reference { + typedef T type; +}; + + +template typename remove_reference::type&& move(T&& t) { + return static_cast::type&&>(t); +} + +template T&& forward(typename remove_reference::type& t) { + return static_cast(t); +} + +template T&& forward(typename remove_reference::type&& t) { + return static_cast(t); +} + +template +unique_ptr make_unique_ptr(Args &&...args) { + return unique_ptr(new T(forward(args)...)); +} + +template void accept_unique_ptr(unique_ptr); // expected-note{{passing argument to parameter here}} + +void test_unique_ptr() { + // Simple construction + unique_ptr p; + unique_ptr p1(new int); + + // Move construction + unique_ptr p2(make_unique_ptr(17)); + unique_ptr p3 = make_unique_ptr(17); + + // Copy construction (failures) + unique_ptr p4(p); // expected-error{{call to deleted constructor of 'unique_ptr'}} + unique_ptr p5 = p; // expected-error{{call to deleted constructor of 'unique_ptr'}} + + // Move assignment + p2 = move(p); + p2 = make_unique_ptr(0); + + // Copy assignment (failures); + p2 = p3; // expected-error{{overload resolution selected deleted operator '='}} + + // Implicit copies + accept_unique_ptr(make_unique_ptr(0.0)); + accept_unique_ptr(move(p2)); + + // Implicit copies (failures); + accept_unique_ptr(p); // expected-error{{call to deleted constructor of 'unique_ptr'}} +} -- 2.11.4.GIT