Name mangling Doubt

S

Subhransu Sahoo

Hi All,

Does the C++ standard tell function overloading can't be done with the
return types of two functions ? If so, is it true that the name
mangling scheme does not take the return type into account ?I know that
C++ standard does not tell anything about name mangling and only talks
bout function overloading. But, I have observed that the Metrowerks
Code Warrior compiler does take return type into the mangling scheme.
Is it not a bug with the compiler ?

Again, the fun part is that the symbols generated are different when I
declare one at a time. But, when I try to declare both it gives error.
I want to know how the compiler knows this in spite of the fact that
the symbols generated are different.

Regards,
Subhransu
 
P

peter koch

Subhransu said:
Hi All,

Does the C++ standard tell function overloading can't be done with the
return types of two functions ?
Yes.

If so, is it true that the name
mangling scheme does not take the return type into account ?

This is unrelated. Any name-mangling might well include the return-type
if any. This would help detecting declaring functions with the wrong
return-type. The code will compile but not link.
I know that
C++ standard does not tell anything about name mangling and only talks
bout function overloading. But, I have observed that the Metrowerks
Code Warrior compiler does take return type into the mangling scheme.
Is it not a bug with the compiler ? Nope.

Again, the fun part is that the symbols generated are different when I
declare one at a time. But, when I try to declare both it gives error.
I want to know how the compiler knows this in spite of the fact that
the symbols generated are different.

This part I don't understand.
Regards,
Subhransu

/Peter
 
W

werasm

Subhransu said:
Hi All,

Does the C++ standard tell function overloading can't be done with the
return types of two functions ?

Yes, it does. See C++98, par 13.1/2:

"Certain function declarations cannot be overloaded:
- Function declarations that differ only in return type...

If so, is it true that the name
mangling scheme does not take the return type into account ?

Can't comment on that. I would think it need not, but OTOH, nothing
prevents it from not.
I know that
C++ standard does not tell anything about name mangling and only talks
bout function overloading. But, I have observed that the Metrowerks
Code Warrior compiler does take return type into the mangling scheme.
Is it not a bug with the compiler ?

No, as the standard does not mention mangling (according to you).
Again, the fun part is that the symbols generated are different when I
declare one at a time. But, when I try to declare both it gives error.
I want to know how the compiler knows this in spite of the fact that
the symbols generated are different.

I'm afraid I'm not a compiler writer :), so I don't know. I do think
that the generation of the symbols have little to do with whether or
not it compiles, though. IOW the lookup-set does not necessarily use
symbol names. But I honestly don't know.

Regards,

Werner
 
G

Gianni Mariani

Subhransu said:
Hi All,

Does the C++ standard tell function overloading can't be done with the
return types of two functions ? If so, is it true that the name
mangling scheme does not take the return type into account ?I know that
C++ standard does not tell anything about name mangling and only talks
bout function overloading. But, I have observed that the Metrowerks
Code Warrior compiler does take return type into the mangling scheme.
Is it not a bug with the compiler ?

Again, the fun part is that the symbols generated are different when I
declare one at a time. But, when I try to declare both it gives error.
I want to know how the compiler knows this in spite of the fact that
the symbols generated are different.

did you try to link two separate files with the same function and
different return values ?

file1.cpp-----

int f()
{
return 1;
}
==============

file2.cpp------

double f()
{
return 1.0;
}
 
T

Tim Slattery

Subhransu Sahoo said:
Hi All,

Does the C++ standard tell function overloading can't be done with the
return types of two functions ?

C and C++ functions can be called without catching their return
values. That is, you can call a function like this:

int i = myfunc(a, b, c);

or like this:

myfunc(a, b, c);

If you've defined two functions with identical signatures except for
return type, and you use the second type of call, the compiler has no
way of determining which function you want to call. That's why you
can't overload a function by changing only its return type.

--
Tim Slattery
(e-mail address removed)
 
A

Andrew Koenig

Does the C++ standard tell function overloading can't be done with the
return types of two functions ?
Yes.

If so, is it true that the name
mangling scheme does not take the return type into account ?I know that
C++ standard does not tell anything about name mangling and only talks
bout function overloading.

You have just answered your own question.
But, I have observed that the Metrowerks
Code Warrior compiler does take return type into the mangling scheme.
Is it not a bug with the compiler ?

Can you write a program that the standard says is correct but the compiler
will not handle it?
Again, the fun part is that the symbols generated are different when I
declare one at a time. But, when I try to declare both it gives error.
I want to know how the compiler knows this in spite of the fact that
the symbols generated are different.

How about an example?
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top