3

I'm trying to construct an object from a function and later pass it to a function that uses it (and consumes it). Here's the code

std::unique_ptr<Object> createObject() {
  auto myobj = std::make_unique<Object>();
  .. modify myobj ..
  return std::move(myobj);
}

void consumeObject(std::unique_ptr<Object>&& robj) {
  myvector.emplace_back(std::forward<std::unique_ptr<Object>>(robj));
}


consumeObject(createObject()); // Is this right?

I'm not using rvalue references directly from the createObject since I would return a reference to deallocated memory.

Is std::move necessary in the part indicated in the code? What happens if, like I did in the code above, I pass a rvalue to be bound to the rvalue reference? The code seems to compile just fine but I can't see the difference between a

consumeObject(createObject());

and

consumeObject(std::move(createObject()));
10
  • 3
    Your code is wrong: createObject returns a unique_ptr<Object>, but consumeObject takes an Object. Can you fix that? Nov 30, 2015 at 11:31
  • 3
    The move in return std::move(myobj); forbids NRVO, simply do return myobj;.
    – Jarod42
    Nov 30, 2015 at 11:39
  • 3
    You're supposed to actually test with the MCVE. Otherwise what's the point in it? (For the record, this isn't a MCVE.) Nov 30, 2015 at 12:45
  • 4
    std::forward makes zero sense in this context (and you forgot to change its template argument in the question). A std::move would do. Nov 30, 2015 at 12:56
  • 1
    @LightnessRacesinOrbit I mostly wanted to joke. I'm very bored and disheartened by my ignorance with move semantics
    – Dean
    Nov 30, 2015 at 13:03

2 Answers 2

8

std::move doesn't do anything magic, it just casts the argument to an rvalue.

Since createObject returns by value, createObject() is already an rvalue (specifically a prvalue), so std::move is unnecessary in this case and you can safely omit it.

1
  • 2
    Once again the utterly blobtastic naming choice for std::move strikes. Nov 30, 2015 at 12:46
3
std::unique_ptr<Object> createObject() {
  auto myobj = std::make_unique<Object>();
  .. modify myobj ..
  return std::move(myobj);
}

You don't need std::move to return a local, move-only object by value.

void consumeObject(std::unique_ptr<Object>&& robj) {
  myvector.emplace_back(std::forward<std::unique_ptr<Object>>(robj));
}

It doesn't make sense* to use std::forward except when you're using so-called "forwarding references", which you're not here. Just having && doesn't mean you should use std::forward. Also you don't need && here at all.

consumeObject(createObject()); // Is this right?

Yes.


Here's what the code probably should look like instead. Note that you mostly avoid any C++11 specific syntax and instead the code looks pretty much the same as passing a copyable object around by value. There's only one place here that anything new is necessary.

std::unique_ptr<Object> createObject() {
  auto myobj = std::make_unique<Object>();
  .. modify myobj ..
  return myobj;
}

void consumeObject(std::unique_ptr<Object> robj) {
  myvector.emplace_back(std::move(robj));
}


consumeObject(createObject());

* Both std::forward and std::move are just casts and the way you use std::forward here happens to work out to the correct cast to get a move construction. However for the sake of readability one should only use std::move to move and only use std::forward to forward.

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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