473,725 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1730
On Mon, 25 Jun 2007 00:32:04 -0700, "su************ **@yahoo.com,
India" <su************ **@yahoo.comwro te:
>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
17874
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. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
5
13152
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 the problem: class BaseA { ...
44
2409
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 statement: void print_structs(void * struct_argument, enum struct_type stype) { switch(stype) { case STRUCT_TYPE_1:
3
2250
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
3884
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
3934
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 parentheses. 1) "The order of evaluation of subexpressions is determined by the precedence and grouping of operators."
3
2142
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 two functions with the same name, this is an error - even when the functions take different parameters, and they would happily exist as overloaded functions if they were declared in the same class instead of being inherited.
4
3376
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
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.