Quote:Original post by Zahlman
There's nothing about the implementation of std::string that requires a std::auto_ptr, BTW, although it is "managed" memory in a sense. There's also nothing in the spec that I know of that would mandate a re-allocation every time - if the string being copied over was longer, there's no reason the old storage couldn't be reused. You're also forgetting about short-string optimizations. And that __FILE__ and __LINE__ stuff is only going to happen in debug mode.
I gave string only as an example.
In C, b is assigned to a. No allocations will ever happen, no magic, no nothing. Either literal value or pointer will be assigned. A memcpy might be involved.
And while it's true that in some cases, some compiler, some STL implementation might under certain circumstances choose to perform certain optimizations, avoid some allocations, might avoid making copies - this is exactly what "behind the scenes" means.
There are obviously optimizations that can happen, but if I give you a line of code, can you say what it does?
Foo bar() { ... return x;};
What does this do in C, and what all can happen in C++? How much memory will be allocated, how long will it take to run, how much code will be generated? What about C# or Java?
There are many acronymed optimizations that might apply to this case, but they are again left at specific compiler's discretion. In this respect, C++ is highly non-deterministic by design.
Quote:Which you can't even create in C.
typedef struct FOO { char * a; char * b; char * c;} foo;foo * make_safe_deep_copy(foo* x);
It can be done quite trivially, but the code in the end will be quite clear, and very explicit about what happens.
And I'm not trying to advocate C as superior, just pointing out why C still tends to be more popular in some domains.