473,395 Members | 1,919 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,395 software developers and data experts.

Is this legal in Standard C++ ?

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++ ?

Jul 23 '05 #1
6 1351
Teddy wrote:
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++ ?


It's ambigous. You compiler just ignores code that isn't used.

Jul 23 '05 #2
In standard C++ you can have one function that has same signature but
in the above case you have 2 functions foo with zero arguments.

Thanks
Shabbir Bhimani
Programming Forums - www.go4expert.com

Jul 23 '05 #3
"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.
--
John Carson

Jul 23 '05 #4
It does not work because you specified default value of i and compiler
does not know which foo() function you want to call, because both can be
call on the same way.

Fibre Optic

Teddy wrote:
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++ ?

Jul 23 '05 #5
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)
Jul 23 '05 #6
Swampmonster wrote:
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.

Neither form is legal. Read 8.3.6 p3:

A default argument expression shall be specified only in the
parameter-declaration-clause of a function
declaration or in a template-parameter (14.1). If it is specified in a
parameter-declaration-clause, it shall
not occur within a declarator or abstract-declarator of a
parameter-declaration.

and then the attached footnote says:

88) This means that default arguments cannot appear, for example, in
declarations of pointers to functions, references to functions, or
typedef declarations.
Jul 23 '05 #7

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

Similar topics

7
by: __PPS__ | last post by:
Actually what I mean is that - if I have some memory buffer, lets say char a; and then I do like this: DWORD num = 0x1234; *(DWORD*)a = num; (1) *(DWORD*)(a+1) = num; (2) either...
11
by: puzzlecracker | last post by:
is it possible to call nonconst member function on an unnamed temprary? ex: class String{ public: void doSomething(); }; String createString();
2
by: Thomas Paul Diffenbach | last post by:
I'm trying to write a space efficient string (nul terminated array of char), storing the characters directly unless the number of characters is too large to be so stored, and storing a pointer to...
11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
2
by: millind | last post by:
I have a small issue. I have a html page which i need to print it legal size. My default printer setting is letter. How can i set a script which will print just this page in legal without change...
30
by: Kiuhnm | last post by:
#include <new> class T { }; int main() { T t = t; T u(u);
17
by: lovecreatesbeauty | last post by:
1. In the following code, is the code (line 11) legal? Is there a notice in the document to tell callers that the parameter s1 should receive an array variable, i.e. type char, but not a variable...
19
by: Sensei | last post by:
Hi! I'm concerned about the legality of such a definition: #define funcX funcY where funcX belongs to the *standard* C functions. Is it legal to do this? The standard says "any function...
36
by: Max | last post by:
This was asked in a C test. Is the following code legal C? Is it legal in C90? C99? #define main() int main #define mainbody () { return 0; } mainbody
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.