Reputation: 1118
I have a question about binary compatibility. I have class A, which includes a public method foo(), and an attribute string _foo;
const string foo() {return _foo;}
When I changed to
const string& foo(){return _foo;}
Is it still binary compatible? Thanks for your help!
Upvotes: 5
Views: 359
Reputation: 129464
Returning a copy of a string will essentially do something like this:
string s = foo();
will, when the compiler generates machine code, appear similar to:
string s;
foo(&s);
Naturally, if you change the type of the return type, the pointer to s
needs to be appropriately modified. So no, you can't change the return type and maintain binary compatibility (and it will almost certainly not even compile, as the signature of the function has changed -> differnet mangled name -> "undefined reference").
Upvotes: 2
Reputation: 179991
It's not even close.
The first version returns a temporary. The move constructor for std::string
can move away from that. The second version returns a reference to _foo
, from which you can't move.
Upvotes: 1
Reputation:
Policies/Binary Compatibility Issues With C++: The goal here is to list the most restrictive set of conditions when writing cross-platform C++ code, meant to be compiled with several different compilers.
The Do's and Don'ts :
...
You cannot... :
Upvotes: 7