For Q1, there is most likely no copy even in C++03 as the copy is removed by the "Named Return Value Optimization" (NRVO).
For Q2, there is also most likely no copy even in C++03 as copy-elision removes it.
For Q3, even in C++11 you do have copies as you would need to mark f1
and f2
as movable in order to actually have them moved:
vector<Frequencies> fs = { std::move(f1), std::move(f2) };
Since you asked multiple question I think I will omit further explanations, look up NRVO, copy-elision and where std::move
is required, ask if you have any further questions.
However, there are cases where you would get free moves, for example if there is a temporary which could be moved:
vector<Frequencies> fs = { buildFrequenciesTable("apple"),
buildFrequenciesTable("bananas") };
The above would detect the two vectors returned from buildFrequenciesTable()
as temporaries and therefore they will be moved into fs
.