Correct me if I'm wrong. Say I have:
struct X
{
std::string mem_name;
X(std::string name)
: mem_name(std::move(name))
{}
...
};
struct Y
{
std::string mem_name;
Y(const std::string &name)
: mem_name(name)
{}
...
};
In X
's ctor, name
is obviously a copy of whatever argument got passed to X
, X
invokes the move ctor of std::string
to initialize mem_name
, right?
Let's call that a copy-then-move on X*; two operations: COPY, MOVE.
In Y
's ctor, name
is a const ref, which means there's no actual copy of the element because we're dealing directly with the argument passed from wherever Y
's object needs to be created. But then we copied name
to initialise mem_name
in Y
; one operation: COPY. Surely it should therefore be a lot faster (and preferable to me)?
In Scott Meyer's GN13 talk (around time-frame 8:10 and 8:56), he talks about "Want speed? Pass by value" and I was wondering is there any performance difference or loss in passing arguments (or strings to be precise) by reference and passing by value "in order to gain speed?"
I'm aware of the fact that passing arguments by value can be expensive, especially when dealing with large data.
Maybe (clearly?) there's something I'm missing from his talk?