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

Overloaded functions by its return type, cast rules


1. Can abybody explain me why C++ function can not be overloaded by its
return type? Return type can has priority higher than casting rules.
For example:

char input(); //can be compiled to name "input$char$void"
int input(); //can be compiled to name "input$int$void"
....

int i= 3+'0'+input(); //can be compiled to:
int(3)+int('0')+input$int$void()
char ch= 3+'0'+input(); //can be compiled to:
char(3)+char('0')+input$char$void()

Are any understandable obstacles exist? There is some profit for such
overloading, for example, we would may not write:
char input_char();
int input_int();

2. Cast is only by return type. Am i right? For example:

A a;
struct B{ static B& operator+ (const A&, const B&); };
B b;
struct B{ static C& operator+ (const B&, const C&); };
C c;
struct D{
static D& operator+ (const D&, const D&);
D(const C&);
};

D d=a+b+c; //give me

d.operator=( D::operator+( D::operator+( D(a)+D(b) ), D(c) ) );
or declared "class_name::operator+" change cast
d.operator=( D( C::operator+ ( B::operator+(a,b), c ) ) );

Or is it FAQ question?

Dec 21 '06 #1
10 2564

"Grizlyk" <gr******@yandex.ruwrote in message
news:11*********************@n67g2000cwd.googlegro ups.com...
>
1. Can abybody explain me why C++ function can not be overloaded by its
return type? Return type can has priority higher than casting rules.
For example:

char input(); //can be compiled to name "input$char$void"
int input(); //can be compiled to name "input$int$void"
...

int i= 3+'0'+input(); //can be compiled to:
int(3)+int('0')+input$int$void()
char ch= 3+'0'+input(); //can be compiled to:
char(3)+char('0')+input$char$void()

Are any understandable obstacles exist? There is some profit for such
overloading, for example, we would may not write:
char input_char();
int input_int();

2. Cast is only by return type. Am i right? For example:

A a;
struct B{ static B& operator+ (const A&, const B&); };
B b;
struct B{ static C& operator+ (const B&, const C&); };
C c;
struct D{
static D& operator+ (const D&, const D&);
D(const C&);
};

D d=a+b+c; //give me

d.operator=( D::operator+( D::operator+( D(a)+D(b) ), D(c) ) );
or declared "class_name::operator+" change cast
d.operator=( D( C::operator+ ( B::operator+(a,b), c ) ) );

Or is it FAQ question?
int foo( std::string& bar )
{
bar = "Hello";
}

bool foo( std::string& bar )
{
bar = "Goodbye";
}

int main()
{
std::string Text;
foo( Text );

std::cout << Text << std::endl;
}

Would that program print "Hello" or "Goodbye"? I believe that's why
functions can't be overloaded by return type alone.
Dec 21 '06 #2

Jim Langston wrote:
int foo( std::string& bar )
{
bar = "Hello";
}

bool foo( std::string& bar )
{
bar = "Goodbye";
}

int main()
{
std::string Text;
foo( Text );

std::cout << Text << std::endl;
}

Would that program print "Hello" or "Goodbye"? I believe that's why
functions can't be overloaded by return type alone.
Maybe, but we always have ambiguouses for overloading
void foo(const int);
void foo(const char);

foo(0); //what calls?

Dec 21 '06 #3

Grizlyk ΠΙΣΑΜ(Α):
void foo(const int);
void foo(const char);

foo(0); //what calls?
No.
void foo(const long);
void foo(const double);
foo(0); //what calls?

It is ambiguous.

Dec 21 '06 #4

Grizlyk wrote:
Grizlyk ΠΙΣΑΜ(Α):
void foo(const int);
void foo(const char);

foo(0); //what calls?

No.
void foo(const long);
void foo(const double);
foo(0); //what calls?

It is ambiguous.
True, but with the following foo() overloads:

void foo(int);
void foo(short);

there would be no ambiguity. foo(int) would be called.

Greg

Dec 21 '06 #5
"Grizlyk" <gr******@yandex.ruwrites:
1. Can abybody explain me why C++ function can not be overloaded by its
return type? k
according to Stroustrup p.151
The reason is to keep resolution for an individual operator or
function call context-independent.
Dec 21 '06 #6
Grizlyk wrote:
Grizlyk ΠΙΣΑΜ(Α):
> void foo(const int);
void foo(const char);

foo(0); //what calls?

No.
void foo(const long);
void foo(const double);
foo(0); //what calls?

It is ambiguous.
Yes, but that ambiguity is easy to resolve by being more explicit about
the type of the argument:

foo(0L); // calls foo(long)
foo(0.0); // calls foo(double)

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 21 '06 #7

Gary Wessle wrote:
according to Stroustrup p.151
The reason is to keep resolution for an individual operator or
function call context-independent.
Yes, but the reason is compiler's reason or human's reason? Maybe there
is dangers in overloaded by return type?

For overloaded functions ambiguouses is always exist, these or those.
Disabling of function overloaded by return type decreases flexibility,
exactly as disabling of function overloaded by parameter type.

Dec 22 '06 #8
On 20 Dec 2006 18:51:11 -0800, "Grizlyk" <gr******@yandex.ruwrote in
comp.lang.c++:
>
1. Can abybody explain me why C++ function can not be overloaded by its
return type? Return type can has priority higher than casting rules.
For example:

char input(); //can be compiled to name "input$char$void"
int input(); //can be compiled to name "input$int$void"
...

int i= 3+'0'+input(); //can be compiled to:
int(3)+int('0')+input$int$void()
char ch= 3+'0'+input(); //can be compiled to:
char(3)+char('0')+input$char$void()
No, it can't, because the types of the expression:

char ch = 3 + '0' + input();

Which are: char = int + char + function return. The rules of C++
require that '0' undergoes the usual arithmetic conversion and
promotes to int.

The fact that you are assigning the value of the expression to a char
has nothing at all to do with the types and conversions in the
expression itself, it never has, and it never will.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 22 '06 #9

Jack Klein ΠΙΣΑΜ(Α):
No, it can't, because the types of the expression:

char ch = 3 + '0' + input();

Which are: char = int + char + function return. The rules of C++
require that '0' undergoes the usual arithmetic conversion and
promotes to int.

The fact that you are assigning the value of the expression to a char
has nothing at all to do with the types and conversions in the
expression itself, it never has, and it never will.
Mm? Is any type of destination not used? It looks as wrong, for there
is no other way to avoid ambiguouse conversions.

Or std types char and short have a special meaning and promotes to int?
Let's consider double instead of char.

Dec 22 '06 #10

Grizlyk wrote:
Gary Wessle wrote:
according to Stroustrup p.151
The reason is to keep resolution for an individual operator or
function call context-independent.

Yes, but the reason is compiler's reason or human's reason? Maybe there
is dangers in overloaded by return type?
It was for the humans. I knew how to get the computer to resolve based
on the return type also - the algorithm was well known at the time. See
"The Design and Evolution of C++" for answers to this kind of questions
- you don't have to guess.

-- Bjarne Stroustrup; http://www.research.att.com/~bs

Dec 22 '06 #11

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

Similar topics

6
by: Melkor Ainur | last post by:
Hello, I'm attempting to build an interpreter for a pascal-like language. Currently, I don't generate any assembly. Instead, I just build an abstract syntax tree representing what I've parsed...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
15
by: Marc Thrun | last post by:
Hello, I've got a few questions: 1) Given the two structs struct A { int x; }; and
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
12
by: Jack Daly | last post by:
I've inherited some code which uses an undocumented feature of a third-party vendor's library. Essentially, this vendor has kept the details of an interface struct secret, but we can pass a pointer...
3
by: Ross | last post by:
I have a problem regarding the precedence of overloaded cast operators. My program is as follows: #include <stdio.h> class A; class B { public: B(A &a) {
1
by: bob | last post by:
I was just wondering if overloaded type cast functions must always be member functions of some class.
1
by: mersinli | last post by:
Hi, I have got seven error and I dont find why compiler find error.. //commission class definition #ifndef COMMISSION_H #define COMMISSION_H #include <string> using std::string;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.