[llvm] r202609 - [C++11] Add support for OwningPtr<T> to be converted to and from
Ahmed Charles
acharles at outlook.com
Sun Mar 2 01:51:44 PST 2014
________________________________
> Date: Sat, 1 Mar 2014 21:55:51 -0800
> Subject: Re: [llvm] r202609 - [C++11] Add support for OwningPtr<T> to
> be converted to and from
> From: richard at metafoo.co.uk
> To: chandlerc at gmail.com
> CC: llvm-commits at cs.uiuc.edu
>
> On Sat, Mar 1, 2014 at 7:38 PM, Chandler Carruth
> <chandlerc at gmail.com<mailto:chandlerc at gmail.com>> wrote:
> Author: chandlerc
> Date: Sat Mar 1 21:38:32 2014
> New Revision: 202609
>
> URL: http://llvm.org/viewvc/llvm-project?rev=202609&view=rev
> Log:
> [C++11] Add support for OwningPtr<T> to be converted to and from
> std::unique_ptr<T>.
>
> Patch by Ahmed Charles!
>
> Modified:
> llvm/trunk/include/llvm/ADT/OwningPtr.h
> llvm/trunk/unittests/ADT/OwningPtrTest.cpp
>
> Modified: llvm/trunk/include/llvm/ADT/OwningPtr.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/OwningPtr.h?rev=202609&r1=202608&r2=202609&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/OwningPtr.h (original)
> +++ llvm/trunk/include/llvm/ADT/OwningPtr.h Sat Mar 1 21:38:32 2014
> @@ -17,6 +17,7 @@
> #include "llvm/Support/Compiler.h"
> #include <cassert>
> #include <cstddef>
> +#include <memory>
>
> namespace llvm {
>
> @@ -39,6 +40,17 @@ public:
> return *this;
> }
>
> + OwningPtr(std::unique_ptr<T> &&Other) : Ptr(Other.release()) {}
> +
> + OwningPtr &operator=(std::unique_ptr<T> &&Other) {
>
> Why an rvalue reference to a unique_ptr rather than passing the
> unique_ptr by value?
No particular reason, though I suppose it is one less write to memory if the optimizer can't remove it. I just didn't consider passing by value. Feel free to change, I don't have commit access yet.
> + reset(Other.release());
> + return *this;
> + }
> +
> +#if LLVM_HAS_RVALUE_REFERENCE_THIS
> + operator std::unique_ptr<T>() && { return std::unique_ptr<T>(take()); }
> +#endif
> +
> ~OwningPtr() {
> delete Ptr;
> }
> @@ -61,6 +73,8 @@ public:
> return Tmp;
> }
>
> + std::unique_ptr<T> take_unique() { return std::unique_ptr<T>(take()); }
> +
> T &operator*() const {
> assert(Ptr && "Cannot dereference null pointer");
> return *Ptr;
More information about the llvm-commits
mailing list