Does adding an arguments with default values to functions break ABI?
Asked Answered
F

1

11

The title says all. I'm going to add an argument to a member function of a class with a default value. The argument is of a non-trivial type. Does this break ABI? Let's say my new library version is going to be M.m.0 and it should be available as a drop-in replacement for all linked applications that use M.m-1.x.

Sample code:

// These are some classes: base and child : public base

/* Version 1.2.3 */
class foo() {
public:
   void do_that_stuff(const std::string a);
}

/* Version 1.3.0 */
class foo() {
public:
   void do_that_stuff(const std::string a, const base& b = base());
}

PS: I did my own test, and it's working. Just can't be sure

Fanchette answered 20/1, 2017 at 12:39 Comment(3)
The name says it all. Default arguments are arguments and have nothing to do with the function type (in particular, with function parameters).Indaba
"Does this break ABI in backwards?" I'm sorry?Bobbette
do_that_stuff will get a different mangled name in old and new compilations - I think that breaks my definition of ABI compatibility.Testify
C
14

Most C++ ABIs encode the argument types of [member] functions in the symbol name. Default arguments are typically implemented as temporary objects conjured up at the point of call. If these are the choices done for the ABI used, adding a default argument will change the ABI. Whether that is the case you'll need to determine with the specific ABI used.

Chlorinate answered 20/1, 2017 at 12:48 Comment(2)
By curiosity, do you know an ABI that would not be broken by this change??Boito
@Oliv: I'm not aware of any. However, an ABI could generate multiple symbols for functions with default arguments. That would be pretty much the approach to change the class without breaking the ABI (assuming the modified function isn't virtual): instead if using a default argument the functions can be overloaded with on calling the other forwarding all argumebts and adding the defaulted argument.Shows

© 2022 - 2024 — McMap. All rights reserved.