[LLVMdev] ErrorOr<> conflicts with unique_ptr<>

David Blaikie dblaikie at gmail.com
Thu Nov 21 16:07:28 PST 2013


On Thu, Nov 21, 2013 at 3:57 PM, Nick Kledzik <kledzik at apple.com> wrote:

> Michael,
>
> In lld, we have places that used nested a ErrorOr<std::unique_ptr<xx>> and
> I often hit compiler errors that require breaking up expressions to work
> around.   Do you have suggestions on how to code the following simple
> examples to not error?  Can some of these be fixed in ErrorOr.h?  Or am I
> totally not getting something?
>
> -Nick
>
>
> struct Foo { void doit(); };
>
>
> std::unique_ptr<Foo> factoryU() {
>   std::unique_ptr<Foo> f(new Foo);
>   return f;  // works as expected
> }
>
> ErrorOr<Foo*> factoryE() {
>   ErrorOr<Foo*> f = new Foo;
>   return f;  // works as expected
> }
>
> ErrorOr<std::unique_ptr<Foo>> factoryEU() {
>   std::unique_ptr<Foo> f(new Foo);
>   return f; // ERROR: call to implicitly-deleted copy constructor of
> 'std::__1::unique_ptr<Foo, std::__1::default_delete<Foo> >’
>

While a local variable inside a function is implicitly moved when returned,
that only happens when the return expression is the local variable and the
same type. In this case you have an implicit conversion that would work
like any other conversion of an lvalue.

So you have to write return std::move(f); unfortunately. (or you could be
more explicit/verbose and say return ErrorOr<...>(std::move(f)); )


> }
>
>
> void sinkU(std::unique_ptr<Foo> f) {
>   f->doit();  // works as expected
> }
>
> void sinkE(ErrorOr<Foo*> f) {
>   f->doit(); // ERROR: member reference base type 'typename
> remove_reference<Foo *>::type' (aka 'Foo *') is not a structure or union'
>

It's questionable whether this should work. ErrorOr<T> models a pointer to
T. So if you had ErrorOr<Foo> f you'd expect to be able to do f->doit(),
but if it's an ErrorOr<Foo*>, jsut like if it were a Foo**, you'd expect to
have to use (*f)->doit().


> }
>
> void sinkEU(ErrorOr<std::unique_ptr<Foo>> f) {
>   f->doit(); // ERROR: no member named 'doit' in
> 'std::__1::unique_ptr<Foo, std::__1::default_delete<Foo> >'
>

Same here.


> }
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20131121/56ab8a70/attachment.html>


More information about the llvm-dev mailing list