Re: std::vector anomally
On Sun, 16 Dec 2007 19:34:40 GMT, "John Keenan"
<john.removeme.keenan@optimapowerware.com> wrote:
"Doug Harrison [MVP]" <dsh@mvps.org> wrote
But for the built-in types, there's no advantage to
pass-by-const-reference
vs. pass-by-value, and it's conventional to use pass-by-value.
Pass-by value requires a copy constructor. Why would I want to make copies
all the way down a call stack?
As I said, I was talking about the built-in types. It's extremely
conventional to write "bool" and "unsigned" vs. the "const bool&" and
"const unsigned int&" from your example.
Why does the standard assignment operator (void operator=(const Xxx&);) use
a const reference?
To avoid copy-constructing the source type for the assignment. (Of course,
the swap idiom for writing an exception-safe assignment operator does
involve the copy ctor.)
We use only reference for calling arguments (if we want a copy we declare a
local variable).
Remember, we're talking about the built-in types...
The arguments can be const or not.
Pass-by-value: Check.
Aside: Did you know these declare the same function?
void f(int);
void f(const int);
From the user's perspective, the "const" is noise, and header files should
use the first one. You can use the second one when you define the function,
but in the real world, this is typically not done as much as it should be.
This is extremelly efficient since only a pointer is passed.
Pass-by-value efficiency: Check. Of course, an int is passed as an int, and
importantly, it's *used* as an int. No dereferencing is required, and the
optimizer doesn't have to worry about aliasing. So that's worth at least a
couple of checkmarks.
Zero code is invoked (no use of copy constructors).
Pass-by-value: Check.
The syntax is straight forward.
Pass-by-value: Check. Actually, it's better.
What motivation is there to use multiple conventions?
Like I said, it's extremely conventional to use pass-by-value for the
built-in types.
--
Doug Harrison
Visual C++ MVP