Search code examples
c++binary-compatibility

Does source incompatibility always imply binary incompatibility?


Any examples demonstrating where source compatibility is broken yet binary compatibility is maintained is welcome.


Solution

  • Old version:

    struct inner {
      int bar;
    }
    
    struct foo {
      struct inner i;
    };
    
    void quux(struct foo *p);
    

    New version:

    struct inner2 {
      int bar;
    };
    
    struct foo {
      struct inner2 i;
    };
    
    void quux(struct foo *p);
    

    Broken code:

    struct foo x;
    struct inner *i = &x.i;
    i->bar = 42;
    quux(&x);
    

    Since the only difference is the name of the struct, and the inner struct's type name is erased during compilation, there's no binary incompatibility.