I'm developing a C++ API and I want to hide private implementation details from the public interface. Currently, I'm employing the Pimpl idiom for this purpose. However, I'm also mindful of minimizing cache misses resulting from pointer indirection. The utmost priority is to keep the private API hidden, and I'm comfortable with users rebuilding their projects if there's an internal API change, for example if it changes in size.
Is there a good solution for this considering newer standards, that not only effectively hide the private implementation but also address the pointer indirection? Additionally, I'm curious about whether there's a way to guide the compiler to automatically expand the size of the public class, allowing all private class members to be allocated within it while considering memory alignment.
As an illustrative example, I'm considering an approach like the one below:
PublicAPI.h
class PublicAPI {
// Public API members
class PrivateAPI;
inline PrivateAPI *imp() const { return (PrivateAPI*)m_imp};
private:
char m_imp[sizeof(PrivateAPI) + padding];
}
PrivateAPI.h
class PublicAPI::PrivateAPI {
// Private API members
}
Of course I want to avoid including PrivateAPI.h in PublicAPI.h. The sizeof(PrivateAPI) + padding is used here for illustrative purposes.