0

If I have two functions like this:

int f ( int a )
{}

void f ( signed a )
{}

are they overloaded?

When in main() I call f(5), I get an error:

old declaration ‘void f(int)’
10
  • you don't really need to call them btw. Jun 11, 2021 at 17:43
  • "Are the functions here overloaded ?" No, these are using the same parameter type effectively. Jun 11, 2021 at 17:44
  • 2
    Don't describe the code that's producing your error. Show the actual code. Jun 11, 2021 at 17:44
  • 7
    int and signed are the same type. You can't overload a function based on its return type alone, because the compiler needs to be able to determine which overload to call based on the arguments provided. Jun 11, 2021 at 17:45
  • 1
    @User Fundamental Types en.cppreference.com/w/cpp/language/types Page down to the table in Integer types Jun 11, 2021 at 17:51

1 Answer 1

2

Type specifiers signed and int (used alone without other type specifiers) define the same signed integer type.

That is you may equivalently write for example

int a;
signed a;
signed int a;
int signed a;

So the functions in the question have the same parameter declaration.

However they have different return types

int f ( int a )
{ }

void f ( signed a)
{}

That is the same function is redefined with different return types. The return type does not take part in function overloading.

So the compiler issues an error that the same function is defined twice with different return types.

From the C++ 14 Standard (13.1 Overloadable declarations)

2 Certain function declarations cannot be overloaded:

(2.1) — Function declarations that differ only in the return type cannot be overloaded

If you would change the parameter declaration in one of the functions for example the following way

int f ( int a )
{ }

void f ( unsigned a)
{}

then the functions would be overloaded and this statement

f( 5 );

would call the first overloaded function because the integer literal 5 has the type int..

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.