A question on function overloading

M

Madhav

Hi All,
I was going through the concept of function overloading and
the different ways of doing it. I thought overloading by return type
could also have been implemented if the creater wanted that. I wanted
to know why this is not part of c++?

Thanks,
Madhav.
 
A

Alf P. Steinbach

* Madhav:
Hi All,
I was going through the concept of function overloading and
the different ways of doing it. I thought overloading by return type
could also have been implemented if the creater wanted that. I wanted
to know why this is not part of c++?

It was probably seen as impractical.
 
C

Clem.Dickey

Madhav said:
may I ask why it was probably seen as impractical?

int f();
double f();
void g(double);
void g(int);

g(f());

Which versions of f and g do you use?
 
K

kayvcai

just take these two function for example

int f(){return 10;}
char f(){return 'A';}

if you have such codes in function main()

f();

How can the complier distinguish them?
So overloading by returning different type is impossible
 
A

Alf P. Steinbach

* (e-mail address removed):
just take these two function for example

int f(){return 10;}
char f(){return 'A';}

if you have such codes in function main()

f();

How can the complier distinguish them?
So overloading by returning different type is impossible

Not impossible, but a tad impractical.

There already is syntax for selecting a function based solely on the
result type, namely disambiguating &f by using a cast.

Cheers,

- Alf
 
M

Mark P

Alf said:
* (e-mail address removed):

Not impossible, but a tad impractical.

There already is syntax for selecting a function based solely on the
result type, namely disambiguating &f by using a cast.

What's the syntax for that? Is it,

static_cast<char (*)()>(f)();
 
A

Alf P. Steinbach

* Mark P:
What's the syntax for that? Is it,

static_cast<char (*)()>(f)();

Looks OK, except of course that without arguments in the function
signature, such overloads cannot currently be defined...

A number of examples of function overload disambiguation is given by
§13.4/4.

A common technique for defining overloads that seemingly differ only in
the result type is to use a dummy pointer argument with default value 0.
That does not work with contextual disambiguation like a cast for the
purpose of calling the function, because the disambiguation requries the
full function signature, so the call must then supply an explicit
argument. However, "overloads" that actually do differ only in the
return type can be defined as template specializations, and then you can
write just f<char>().
 
J

Jerry Coffin

@v46g2000cwv.googlegroups.com>, (e-mail address removed)
says...
Hi All,
I was going through the concept of function overloading and
the different ways of doing it. I thought overloading by return type
could also have been implemented if the creater wanted that. I wanted
to know why this is not part of c++?

Consider what happens if you ignore the returned value:

int x() {}
char x() {}

int main() {
x();
return 0;
}

Which one should be called? There are also major problems
dealing with implicit conversions, such as:

double d = x();

calling one of the functions above.
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,146
Latest member
Vinay KumarNevatia_
Top