I have some pre-C++11 code in which I
use const
references to pass large parameters like
vector
's a lot. An example is as
follows:
int hd(const
vector& a) {
return
a[0];
}
I
heard that with new C++11 features, you can pass the vector
by
value as follows without performance
hits.
int
hd(vector a) {
return
a[0];
}
For
example, this
answer says
C++11's move semantics make passing and returning by value much more attractive
even for complex
objects.
Is
it true that the above two options are the same
performance-wise?
If so, when is using const
reference as in option 1 better than option 2? (i.e. why do we still need to use const
references in C++11).
One reason I ask is that
const references complicate deduction of template parameters, and it would be a lot
easier to use pass-by-value only, if it is the same with const reference
performance-wise.
No comments:
Post a Comment