Domanda

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!

È stato utile?

Soluzione 2

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").

Altri suggerimenti

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... :

  • changing the return type in any way

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top