John Carson wrote:
"Teddy" <du********@hotmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com
Hello all
simple code below:
void foo() {
}
void foo(int i = 0) {
}
int main()
{
//foo(); // call of overloaded 'foo()' is ambiguous
}
The code is successfully compiled on gcc 3.3.3
But when I added "foo();" in main(), gcc gave this error: call of
overloaded 'foo()' is ambiguous.
Is this overload legal in Standard C++ ?
Strictly speaking, the overload is legal, but you can't use it in such a
way that there is ambiguity about which one you mean. If you call
foo(5);
then there is no ambiguity and the code will compile. With
foo();
the compiler doesn't know which one you mean, so it doesn't compile.
BTW: Is the following standard C++ code? It compiles with MSVC 7.1
with extensions disabled and without warning. And it runs fine.
So I think it just might be - only the "static_cast< void(*)
(int=4) >(&foo)();" line puzzles me a little, since MSVC complains
about the "void (*p3)(int=2) = &foo;" line which looks very very
similar to me.
#include <iostream>
void foo()
{
std::cout << "foo()" << std::endl;
}
void foo(int x=0)
{
std::cout << "foo(" << x << ")" << std::endl;
}
int main()
{
void (*p)() = &foo;
void (*p2)(int) = &foo;
// MSVC 7.1 doesn't like the line below
// void (*p3)(int=2) = &foo;
p();
p2(1);
// see above
// p3();
static_cast< void(*)() >(&foo)();
static_cast< void(*)(int) >(&foo)(3);
static_cast< void(*)(int=4) >(&foo)();
return 0;
}
---------
When executed it prints the following, just as expected:
foo()
foo(1)
foo()
foo(3)
foo(4)