std::vector::emplace_back with lvalue expression
As you already said, passing const S&
would just invoke the copy constructor.
Unless you intend to use s
in some way before passing it to emplace_back
, it is therefore not necessarily wise.
However, if the code to create s
was, for instance, exceptionally long, it could improve readability to put it and the code for emplace_back
on separate lines. Compilers are extremely good at optimizing such cases and will probably generate the same code anyways (if the copy constructor is default). Basic example: https://godbolt.org/z/D1FClE
If it improves readability or maintainability do it, otherwise there’s no value in it.