We have a struct in revision 1 of a shared library that we need to maintain the ABI for:
struct Person
{
std::string first_name;
std::string last_name;
}
In the revision 2, we're changing Person to this:
class Person
{
public:
Person(const std::string &f, const std::string &l);
std::string first_name;
std::string last_name;
}
To maintain source compatibility, we'd like to modify reversion 1 of Person so that code compiled against newer header files will run and code not recompiled will run.
Can we do the following with two new non-inline constructors:
class Person
{
public:
Person();
Person(const std::string &f, const std::string &l);
std::string first_name;
std::string last_name;
}
We're doing this all with g++. In looking in the generated shared library with nm, I don't see a constructor or destructor for the plain struct, so I'm guessing that code that is not recompiled will just construct the Person at the calling site as before which is fine. Any code that is recompiled will use the no-arg constructor.
The only problem I see is if we need to roll back to an older version of the shared library that doesn't have the constructors, then any code compiled against it will break, but I'm not concerned about this situation.