10

I have base object called IList. Then I have VectorList, that inherits IList.

then I have function like this:

std::unique_ptr<IList> factory(){
    auto vlist = std::make_unique<VectorList>();
    return vlist;
}

This compiles without problem under gcc, but clang gives following error:

test_file.cc:26:9: error: no viable conversion from 'unique_ptr<VectorList, default_delete<VectorList>>' to
      'unique_ptr<IList, default_delete<IList>>'
        return vlist;

How is proper way to handle this kind of errors?

15
  • 4
    Could you provide an mcve?
    – Chris Drew
    Sep 23, 2015 at 14:50
  • 2
    @CoryKramer The question explicitly states that. "I have base object called IList. Then I have VectorList, that inherits IList." Sep 23, 2015 at 14:52
  • 1
    @Angew I agree, but you shouldn't have to explicitly std::move during a return Sep 23, 2015 at 14:56
  • 3
    Don't forget the virtual destructor for this to work correctly with unique_ptr.
    – Rostislav
    Sep 23, 2015 at 14:57
  • 3
    @CoryKramer The point here is that the type of vlist is not the same as the return type. vlist is unique_ptr<VectorList>, the return type is unique_ptr<IList>. So that clause of the standard ("try a move first") does not apply. Sep 23, 2015 at 15:00

1 Answer 1

16

It appears (your version of) Clang is still following C++11 behaviour in this regard. In C++11, you had to use std::move in this case, because the type of vlist is different from the return type, and so the clause of "when returning an lvalue, try it as an rvalue first" did not apply.

In C++14, this restriction of "same types required" was lifted, and so in C++14, you shouldn't need the std::move in the return statement. But if you need your code to compile with your current toolchain, simply add it there:

return std::move(vlist);

The exact C++11 wording was this:

12.8/32 When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue. ...

The criteria for copy elision (which include "same type") have to be met; they're just slightly extended to cover parameters as well.

In C++14 (N4140), the wording is broader:

12.8/32 When the criteria for elision of a copy/move operation are met, but not for an exception-declaration, and the object to be copied is designated by an lvalue, or when the expression in a return statement is a (possibly parenthesized) id-expression that names an object with automatic storage duration declared in the body or parameter-declaration-clause of the innermost enclosing function or lambda-expression, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.

(Emphasis mine)

As you can see, copy elision criteria are no longer required for the return case.

13
  • @Barry I've added a quote from N4140. They didn't change copy elision requirements, but relaxed those on return statements. Sep 23, 2015 at 15:09
  • 1
    then this should not compile using g++ -std=c++11, right?
    – m.s.
    Sep 23, 2015 at 15:14
  • 1
    @Barry I believe that the "or" has weaker binding than the "and", i.e. I read it as "When the criteria for copy elision are met.... or when the expression in..."
    – Columbo
    Sep 23, 2015 at 15:31
  • 4
    @Columbo Oh so (Criteria Met And Not Exception And Lvalue) Or (Return Statement Names Object)? This is why we don't write 82-word sentences, boys and girls.
    – Barry
    Sep 23, 2015 at 15:34
  • 2
    @Angew Well, nobody is selling a "conforming C++11" certification for compilers. It basically means that you can expect to see compilers implement the C++14 behavior even in their C++11 modes.
    – T.C.
    Sep 24, 2015 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.