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)’
abhishek_naik
  • 1,287
  • 2
  • 15
  • 27
User
  • 23
  • 6
  • you don't really need to call them btw. – apple apple Jun 11 '21 at 17:43
  • _"Are the functions here overloaded ?"_ No, these are using the same parameter type effectively. – πάντα ῥεῖ Jun 11 '21 at 17:44
  • 2
    Don't describe the code that's producing your error. Show the actual code. – Drew Dormann Jun 11 '21 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. – Nathan Pierson Jun 11 '21 at 17:45
  • @ Nathan Yes that is a point I know the concept of overloading but don't know that type signed and int is the same . – User Jun 11 '21 at 17:49
  • @User it's a holdover from `C` "everything" was `int` if you left out some of the type declaration. – Richard Critten Jun 11 '21 at 17:51
  • Where can I find the reference of different and same data type . I searched but couldn't find a reference for them . Thanks a lot – User Jun 11 '21 at 17:51
  • 1
    @User Fundamental Types https://en.cppreference.com/w/cpp/language/types Page down to the table in __Integer types__ – Richard Critten Jun 11 '21 at 17:51
  • @Richard thanks . But excuse me what do you mean by everything" was int if you left out some of the type declaration – User Jun 11 '21 at 17:53
  • @User it not something you want to remember (or even learn) but in old `C` you could just call a function (without declaring it) and the compiler would assume it was of the form `int fun(int)` also `signed` is `signed int`; `short` is `short int` etc – Richard Critten Jun 11 '21 at 17:55

1 Answers1

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..

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335