[Cs3] Const parameters
Mikhail Nesterenko
mikhail at cs.kent.edu
Sun Sep 27 12:21:32 EDT 2015
>
> Talking about move's efficiency got me wondering if there's a point to a
> pass-by-value of a parameter that's specified as const.
> fn(const &myVar) saves a copy from being made, and appears to be as safe
> and predictable as the copy version. I'm not really versed in threads yet,
> but is there some worry that during the function's execution, the reference
> argument might be changed by some other thread? If not, seems to me that
> const parameters should always be passed by reference.
For basic types an pointers, pass by value is more efficient. In pre
c++11, for non-basic types or pointers, unless you are explicitly
needing to make a copy in the parameter, pass by const reference was
recommended to pass by value. By for example such experts as Scott
Meyers in his Effective C++.
In some cases making a copy is useful, for example for copy-and-swap
implementation of assignment overloading.
However, there is this well-known article
http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/
on pre-C++11 language which introduces so-called copy-elision:
compiler replacing a copy of the r-value by moving the object.
in C++11, this is explicitly taken advantage of by move-semantics.
Thanks,
--
Mikhail
More information about the cs3
mailing list