Re: Saving a binary file into a string
On Dec 28, 1:07 am, Kaz Kylheku <kkylh...@gmail.com> wrote:
void SaveFile(string FilePath, string FileContent)
void SaveFile(const string& path, const string& s)
Under any sanely implemented compiler, there is no difference.
So this is like writing x >> 2 instead of x / 2.
The caller passes pointers to the objects; the callee will make copy
if it modifies the arguments, otherwise it works with the caller's
objects directly.
Really? This surprises me, considering this is the first I've heard
about such magics, and many longtime posters here seem to assume /
know otherwise. Division by two vs bitshifting is very simply,
provably equivalent code, but changing a pass by value to a pass by
const ref is much more involved, and I believe the analogy does not
work.
So, how would this be implemented and still have a separate
compilation model?
Ex:
//
#include <iostream>
using namespace std;
class foo
{
public:
foo() { cout << "foo()" << endl; }
foo(const foo& ) { cout << "foo(const foo& )" << endl; }
foo& operator= (const foo& ) { cout << "foo& operator= (const foo& )"
<< endl; return *this; }
~foo() { cout << "~foo()" << endl; }
};
void passByValue(foo ) {}
int main()
{
foo x;
passByValue(x);
}
//
I think that this example must have exactly one ctor call, one copy
ctor call, and 2 dtor calls. I don't see how you can change it and
still have it be standard compliant.
On a more general program, barring whole program optimization with the
standard's "as if" rule, that is still with separate compilation, I
don't see how you can do it.
Even if the compiler is stupid to actually copy the objects in argument
passing, the implementation of std::string can prevent copying the
actual data by managing references.
Except it might not do copy on write strings because that can be more
expensive for certain applications. That's a tradeoff optimization,
not a globally better one.