Ambiguity while overloading functions


B

bintom

The following program uses overloaded functions. It works OK. But if
the commented function is made part of the program, the compiler
complains about ambiguity. Is this a case of of integer getting
promoted to float? Thanks in advance.

Bintom



void func(int i)
{ cout << "In func(int)\n\n"; }

/*
void func(float f)
{ cout << "In func(float)\n\n"; }
*/

void func(double d)
{ cout << "In func(double)\n\n"; }

int main()
{ func(3);
func(3.3);
}
 
Ad

Advertisements

S

Sjouke Burry

bintom said:
The following program uses overloaded functions. It works OK. But if
the commented function is made part of the program, the compiler
complains about ambiguity. Is this a case of of integer getting
promoted to float? Thanks in advance.

Bintom



void func(int i)
{ cout << "In func(int)\n\n"; }

/*
void func(float f)
{ cout << "In func(float)\n\n"; }
*/

void func(double d)
{ cout << "In func(double)\n\n"; }

int main()
{ func(3);
func(3.3);
}
You commented out the float function, why?
 
I

Ian Collins

The following program uses overloaded functions. It works OK. But if
the commented function is made part of the program, the compiler
complains about ambiguity. Is this a case of of integer getting
promoted to float? Thanks in advance.

Which complier and what is the warning?

In your posted code there shouldn't be any ambiguity between a floating
literal and an integer literal.
Bintom



void func(int i)
{ cout<< "In func(int)\n\n"; }

/*
void func(float f)
{ cout<< "In func(float)\n\n"; }
*/

void func(double d)
{ cout<< "In func(double)\n\n"; }

int main()
{ func(3);
func(3.3);
}

3 is an integer literal, it's type defaults to int. 3.3 is a floating
literal, which defaults to double. So the int and double overloads will
be called. func(3.3f) will call the float overload.
 
J

Juha Nieminen

bintom said:
void func(int i)
{ cout << "In func(int)\n\n"; }

/*
void func(float f)
{ cout << "In func(float)\n\n"; }
*/

void func(double d)
{ cout << "In func(double)\n\n"; }

int main()
{ func(3);
func(3.3);
}

At least gcc doesn't complain about anything, even with the second
function uncommented.
 
B

blackbug

as said above

the int changes double by default until you mention something lie 2.2f
or so.
 
Ad

Advertisements

I

Ian Collins

double d = 3;

The type of "3" here doesn't default to int, it simply *is* int.
Granted, that's a rather subtle point, but defaults can be overridden;
the type of "3" can't be. You can use a literal constant with a
different type, such as "3U" or "3L", but that's replacing, not overriding.

It's an important conceptual difference, but not germane to the specific
question under discussion.
Fair enough, but in the context I was replying to:

int main()
{ func(3);
func(3.3);
}

my response was correct. The 3 in func(3) is an integer literal and the
3.3 in func(3.3) is a floating literal. Neither has a type suffix, so
the default type rules apply.
 
Ö

Öö Tiib

Fair enough, but in the context I was replying to:

int main()
{ func(3);
   func(3.3);

}

my response was correct.  The 3 in func(3) is an integer literal and the
3.3 in func(3.3) is a floating literal.  Neither has a type suffix, so
the default type rules apply.

So you did mean "defaults to" is same as "without suffix is". Then it
is not correct. Standard has quite verbose rules how type of integer
literal without suffix is determined so nothing is considerable as
default there. Don't worry, I also mix up such terms all the time.
 
I

Ian Collins

So you did mean "defaults to" is same as "without suffix is". Then it
is not correct. Standard has quite verbose rules how type of integer
literal without suffix is determined so nothing is considerable as
default there. Don't worry, I also mix up such terms all the time.

The actual wording is:


"If it is decimal and has no suffix, it has the first of these types
in which its value can be represented: int, long int; if the value
cannot be represented as a long int, the behavior is undefined."

The integer literal 3 can be represented as int, so 3 "without suffix
is" an int.
 
Ad

Advertisements

J

Johannes Schaub (litb)

Ian said:
The actual wording is:


"If it is decimal and has no suffix, it has the first of these types
in which its value can be represented: int, long int; if the value
cannot be represented as a long int, the behavior is undefined."

No, your use of "default" is wrong. I agree with Pete here. There is no
'default'. That's a keyword.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top