I am learning PIMPL idiom. One of its advantage is binary compatibility. I am wondering what the advantages of binary compatibility are. Thanks!
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
v1.0
Lets consider the following class in a v1.0 of
libMagic
libraryApplication code:
When the above application code is compiled by dynamically linking against
libMagic.so
,foo
function is compiled as followsv1.0.1
Now, when libMagic releases a new version v1.0.1 with the below change in implementation but no change in header files
application does not have to recompile and hence need not be updated. It will automatically call the updated version with new implementation.
v1.1.0 - Binary incompatible
Lets say there is another update to the library (v1.1.0) with below changes.
Now, the compiled
foo
function will not allocate space for the new member added. The library has broken binary compatibility.What happens is undefined behavior. Likely
i=27
will write on the cache variable andMagicNumber::get
will return 27. But anything could happen.If
libMagic
had used PIMPL idiom, All member variables will belong toMagicNumberImpl
class whose size is not exposed to application code. So library authors could add new members in later versions of library without breaking binary compatibility.The above class definition will not change in new versions and size of a pointer does not change when new members are added to a class.
Note: Binary compatibility is a concern only in the following cases
.so
file in linux).Note2: There is another way to solve the same problem without using PIMPL - ABI versioning of namespaces.
It avoids the Fragile Binary Interface Problem. It goes like this:
Program uses library.
User upgrades library. Upgrade changes something in the library's binary interface.
Program now doesn't work until it is recompiled because it was built to the old binary interface.
One of the advantages of the PIMPL idiom is that it allows you to move things that would normally be part of the public interface of a class into its private interface (in fact, into the interface of a private class). You can change the private interface without breaking binary compatibility.
The advantage of the PIMPL idiom is not so much binary compatibility but rather the reduced need for recompilation if you change the implementation or even the layout of a class. For example, if you add a new data member to a class, that changes the layout of the class and you would normally need to recompile all clients of the class, but not if you use the PIMPL idiom.
Binary compatibility is more about being compatible with multiple compilers (and compiler versions), and the only way to do that in C++ is with interfaces (abstract classes) that are implemented by classes not exposed to clients. This is because the vtable layout of abstract classes is implemented identically by all compilers. Many APIs, such as the DirectX APIs, are exposed this way so that they can be used with any compiler.