Re: STL Vector - pass by reference?
Doug Harrison [MVP] wrote:
:: On Fri, 10 Aug 2007 08:59:05 +0200, Ulrich Eckhardt
:: <eckhardt@satorlaser.com> wrote:
::
::: Gerry Hickman wrote:
:::: Doug Harrison offered this example:
::::
:::: ----- example start -----
::::
:::: I would use pass-by-reference to avoid this needless cost, e.g.
::::
:::: vector<string>::size_type
:::: void GetDeviceClasses(vector<string>& guids)
:::: {
::: [...]
:::: returns guids.size();
:::: }
:::
::: Funny. Talking about needless costs and then returning redundant
::: data - the size can be retrieved from the vector.
::
:: What's "funny" is to compare returning a vector<string> by value to
:: returning its size, the latter being as close to a free operation
:: as there is, both in terms of complexity and exception safety. Not
:: to mention, returning the size is a useful thing to do.
How do you know that it is useful? Always?
Assuming that we are trying to optimize a time-critical piece of code,
why return a value that isn't asked for? Especially as it is also
returned as a part of the guids parameter.
It is also often not considered good style to have both a return value
and out-parameters. If you need to, you can have several
out-parameters, and not single out one of them as a return value.
Bo Persson