473,324 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

ambiguity related to overloaded functions

Consider the program

#include <iostream>

using namespace std;

void fn(const char * str, char x = 'Y')
{
cout << "from fn(const char *, char) - " << str << endl;
return;
}

void fn(const char * str)
{
cout << "from fn(const char *) - " << str << endl;
return;
}

int main( )
{
// fn("test string");
return 0;
}

This program compiles without any error or warning. If I remove the
comment in the statement
// fn("test string");
in main( ), I am getting ambiguity error. Why doesn't the compiler
detect the ambiguity even when the call to fn( ) was not made ? What
is the expected behaviour ? Where am I going wrong ?

Kindly explain.

Thanks
V.Subramanian

Jun 25 '07 #1
2 1706
On Mon, 25 Jun 2007 00:32:04 -0700, "su**************@yahoo.com,
India" <su**************@yahoo.comwrote:
>Consider the program

#include <iostream>

using namespace std;

void fn(const char * str, char x = 'Y')
{
cout << "from fn(const char *, char) - " << str << endl;
return;
}

void fn(const char * str)
{
cout << "from fn(const char *) - " << str << endl;
return;
}

int main( )
{
// fn("test string");
return 0;
}

This program compiles without any error or warning. If I remove the
comment in the statement
// fn("test string");
in main( ), I am getting ambiguity error. Why doesn't the compiler
detect the ambiguity even when the call to fn( ) was not made ? What
is the expected behaviour ? Where am I going wrong ?
Ambiguity condition is tested only when the function is needed, that
is, when compiler tries to find an adequate funtiona for the call
If commented line were:
fn("test string",'N');
there would be no ambiguity at all.

Best regards,

Zara
Jun 25 '07 #2
su**************@yahoo.com, India wrote:
Consider the program

#include <iostream>

using namespace std;

void fn(const char * str, char x = 'Y')
{
cout << "from fn(const char *, char) - " << str << endl;
return;
}

void fn(const char * str)
{
cout << "from fn(const char *) - " << str << endl;
return;
}

int main( )
{
// fn("test string");
return 0;
}

This program compiles without any error or warning. If I remove the
comment in the statement
// fn("test string");
in main( ), I am getting ambiguity error. Why doesn't the compiler
detect the ambiguity even when the call to fn( ) was not made ? What
is the expected behaviour ? Where am I going wrong ?

Kindly explain.

Thanks
V.Subramanian
Because functions themselves are not ambiguous. Calls to functions can
be. Even in this program it would be possible to call the second
version of fn unambiguously. One method of doing so might be:
void (*p)(const char * str) = fn ;
p("test string") ;
--
Alan Johnson
Jun 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: IvD² | last post by:
During a project I ran into trouble when using multiple inheritance. I was able to resolve the problem, but was unable to really understand the reasons for the error. Here is a short example of...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
54
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of...
3
by: Adam Nielsen | last post by:
Hi everyone, I've run into yet another quirk with templates, which IMHO is a somewhat limiting feature of the language. It seems that if you inherit multiple classes, and those classes have...
4
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.