Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does reordering public non-virtual methods in a stand-alone class break ABI?

Does changing the order of public non-virtual non-inline overloaded methods in a stand-alone class break the ABI?

Before:

class MyFinalClass
{
public:
    // ...
    void doSomething(char c, int i, int n);
    void doSomething(char c, int i);
    // ...
};

After:

class MyFinalClass
{
public:
    // ...
    void doSomething(char c, int i);
    void doSomething(char c, int i, int n);
    // ...
};

Thanks!

like image 646
Víctor Fernández Avatar asked May 10 '12 15:05

Víctor Fernández


People also ask

Can you override non-virtual functions?

By default, methods are non-virtual, and they cannot be overridden. Virtual modifiers cannot be used with static, abstract, private, and override modifiers.

What is non-virtual class?

The non-virtual interface pattern (NVI) controls how methods in a base class are overridden. Such methods may be called by clients and overridable methods with core functionality. It is a pattern that is strongly related to the template method pattern.


2 Answers

The functions are linked by their name and signature, not by their position in the class. So no, you're not breaking the ABI.

Virtual functions are a different matter because they're linked by their position in a vtable (usually). This won't be a problem if you consistently recompile every file that depends on the header that defines the order, but if the class exists in a library it could be a concern.

like image 198
Mark Ransom Avatar answered Oct 15 '22 20:10

Mark Ransom


There are two things that breaks the ABI when you update your classes. Virtual functions as pointed out by Mark (and remember that it is not because you did not mark a function as virtual that it isn't.)

The other things are inline functions because those make use of your variable members. If the order of your variable members changes then those inline's that were compiled in other software break too.

As pointed out by Russel Greene (see comment) the order of variable members can also cause alignment differences which is enough to change the size of the allocated objects. That also breaks your allocation (assuming the client's code uses new)

And adding/removing members may not break the ABI (you still could allocate a byte here or a word there...) but that's usually a deal breaker right there.

like image 37
Alexis Wilke Avatar answered Oct 15 '22 20:10

Alexis Wilke