Ah cool. I'm still a little bit shakey on move semantics so I don't see myself using v.push_back(std::move(obj)).
|
That's okay. You get the benefits of move semantics for free when you write
v.push_back(f())
if
f() returns by value, for example.
The operation
Move performs the same function as
Copy; except that the source of the move may be modified.
Can I achieve the same end using v.push_back(std::move(s)) |
No.
push/insert always requires either a copy or move, whereas
emplace requires neither a copy nor move in general. Consider
fset.emplace(3, "three", 3.0);
This doesn't copy or move any Foo object, because none exists until
emplace constructs it
directly within the set's data structure.
Contrast this with
fset.insert(Foo{3, "three", 3.0});
Since
insert expects a reference to
Foo as an argument, one must be created to initialize the reference parameter. Inside
insert, the referent must be used to move-construct the
Foo within the set's data structure.