Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 904
sorush-r Avatar asked Jan 20 '17 12:01

sorush-r


People also ask

Can functions have default arguments?

Default arguments are only allowed in the parameter lists of function declarations and lambda-expressions, (since C++11) and are not allowed in the declarations of pointers to functions, references to functions, or in typedef declarations.

How does default arguments are useful in functions?

Default arguments are useful when we want to increase the capabilities of an existing function as we can do it just by adding another default argument to the function. It helps in reducing the size of a program. It provides a simple and effective programming approach.

Can we pass default arguments to overloaded functions?

No you cannot overload functions on basis of value of the argument being passed, So overloading on the basis of value of default argument is not allowed either. You can only overload functions only on the basis of: Type of arguments. Number of arguments.

Which are the rules for default arguments?

A default argument is a value in the function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument. The values passed in the default arguments are not constant. These values can be overwritten if the value is passed to the function.


1 Answers

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.

like image 146
Dietmar Kühl Avatar answered Oct 19 '22 04:10

Dietmar Kühl