Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

overloading function matching

17 views
Skip to first unread message

sjsun...@gmail.com

unread,
May 24, 2016, 2:50:37 AM5/24/16
to
hello

asking one question about simple overloading function matching

let me show some code snippet
--------------------------------------------------------------
#include <iostream>
#include <string>
using namespace std;
string f(int n)
{
cout<<"int function called : "<<n<<endl;
return "done";
}
string f(unsigned int n)
{
cout<<"unsigned int called : "<<n<<endl;
return f(3);
}
int main(void)
{
f(12);
f(12U);

return 0;
}

this code results as follow
---------------------------------------------------
int function called : 12
unsigned int called : 12
int function called : 3

yeah this is simple
f(12) called f(int), and f(12U) called f(unsigned int) which again called f(int) inside of it

these was plain..

but if i change the function definition code for two f function like this

#include <iostream>

using namespace std;

string f(unsigned int n) //function definition place was changed
{
cout<<"unsigned int called : "<<n<<endl;
return f(3);
}

string f(int n) //function definition place was changed
{
cout<<"int function called : "<<n<<endl;
return "done";
}

int main(void)
{
f(12);
f(12U);

return 0;
}

and if i run those codes.. it result stack overflow : it was infinite recursive calling

very strange : in main f(12U) called unsigned int version of f
but when that unsigned int version function called f(3)
it called unsigned int version of f instead of int version of f

I searched overloading function matching rule.. ( 1. exact matching, 2. promotion, 3. standard conversion..) but can't understand that results


Christian Gollwitzer

unread,
May 24, 2016, 3:10:38 AM5/24/16
to
Am 24.05.16 um 08:50 schrieb sjsun...@gmail.com:
> #include <iostream>
>
> using namespace std;
>

string f(int n);

> string f(unsigned int n) //function definition place was changed
> {
> cout<<"unsigned int called : "<<n<<endl;
> return f(3);
// here the compiler does not know that there is an f(int) function.
// Unless you have the forward declaration shown above.
Message has been deleted

sjsun...@gmail.com

unread,
May 24, 2016, 3:32:22 AM5/24/16
to
thanks Christian Gollwitzer

that was correct answer!!
0 new messages