0

If a function with the same name is defined in multiple files with a different signature, are these overloaded eg.

File1.cpp

int foo(){//do something}

File2.cpp

int foo(int a){//do something}

If I compile both these files in the same project, will these functions be treated as overloaded functions.

2 Answers 2

2

Yes they are two different functions.

A functions signature is primarily based on the arguments: The number of arguments, their types and the order. Class member functions also have modifiers, like const or not. Return type is not part of the signature to distinguish between overloaded functions.

If two functions of the same name have unique signatures, then they are different.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, they will be appropiately name-mangled into two separate functions whose scope is global, at the global namespace (a.k.a: the :: namespace).

For two functions to be overloaded, and not violate the ODR, their parameter types and "attributes" (such as const, volatile, or noexcept after the argument list) shall be different. Two functions with the same set of parameter types and "attributes", but with different return type violate the ODR.

Just don't worry about this, and let the linker be the slave of the compiler it was born to be.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.