1

I have a question about changing parameter name of a member function.

I have function foo(type iA) then I change to foo(type iB), I think it is bin comp, but I am not sure.

Could you help me to explain the reason? Thanks a lot!

luk32
  • 15,812
  • 38
  • 62
CJAN.LEE
  • 1,108
  • 1
  • 11
  • 20

2 Answers2

3

The names of the arguments is just for you to distinguish the arguments. You can have different names in a function declaration and the functions definition.

So this is okay:

void foo(int bar);  // Declare function

...

// Define function
void foo(int bibibibibi)
{
    ...
}

The declaration and definition above is for the same function. C++ does not override or overload functions based on argument names, only argument types.

The compiler does not store the names of arguments (or other variable) except as debug info. The actual generated code have no information about the variable names.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

Of course it will be binary compatible because you do not change the type of object. Hence the size stays the same. Name of variable is only for human.

You can actually declare function not giving any names, like:

int funct(void*, int, Object)

When you define it you can use whatever you like

int funct(void* ptr, int something , Object object){return 42;};

It is fine. It also works for class members.

The (I would say main) reason for binary compatibility or incompatibility are the sizes of passed objects. So when you operate on actual binary data, on assembler level no offsets change. If you changed types, arguments would lie in different places in memory, and offsets for them would need to be recalculated. What is important are addresses not aliases or names.

Edits:

For the sake of the completeness. As @Lightness pointed out, you can also skip names in definition, so the very example I gave could look: int funct(void*, int, Object){return 42;};.

Also regarding @James' comment. I would say that when two objects have the same size, it gets murky. Of course it makes no sense to make such wild conversion from one type to another. However, considering previous example, if you do not use operand it would probably work ... on the assembler level. If sizes differed it would corrupt stack.

However, when I thought how to comment on this problem, I told myself that you ask about c++. So let's look at it from this point of view. I would say that functions are binary compatible when their signature does not change. And names of operands are not part of it in any definition. Function signature is a basis for name mangling and overloading, and the symbol name in the final binary object.

luk32
  • 15,812
  • 38
  • 62
  • It's not just size. On my platform, `float` and `int` have the same size, but replacing one with the other will break binary compatibility. – James Kanze Nov 14 '13 at 09:41
  • You can define a function without any names, too – Lightness Races in Orbit Nov 14 '13 at 11:38
  • @LightnessRacesinOrbit I supposed that if you do not need to use variable, then probably skipping name would be fine in declaration. I was honestly too lazy to check it, as it's really a borderline case. – luk32 Nov 14 '13 at 11:40
  • @JamesKanze I edited answer trying to reflect on your comment. IMO it's hard to talk about binary compatibility per-se when it comes to pure c++ since different compilers might produce binary incompatible code as the ABI is not standarised. I hope you like it better now. And that I understand everything good. I think signature is best shot, not sure what happens to the return type though. But I am no expert on linkers. – luk32 Nov 14 '13 at 12:42