473,406 Members | 2,343 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,406 software developers and data experts.

Is it possible to overload with return values?

Hi,
Here I am taking two functions. void f(int,int) and another one is
float f(int,int).
Is it possible to overload with return values.

Thx,
kishore

Dec 29 '05 #1
18 2424
sk*******@yahoo.co.in wrote:
Hi,
Here I am taking two functions. void f(int,int) and another one is
float f(int,int).
Is it possible to overload with return values.


No.
C++ Standard (Working Draft, 2005-10-19) says:

13.1 Overloadable declarations
2 Certain function declarations cannot be overloaded:
— Function declarations that differ only in the return type cannot
be overloaded.
Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 29 '05 #2
No, it's not possible to overload functions based on its return type.

Ex:

void f(int);
int f(int);
main()
{
f(); // How does the compiler know to call which function in
this case.
}
sk*******@yahoo.co.in wrote:
Hi,
Here I am taking two functions. void f(int,int) and another one is
float f(int,int).
Is it possible to overload with return values.

Thx,
kishore


Dec 29 '05 #3
Sunil Varma wrote:
No, it's not possible to overload functions based on its return type.


There is one thing permitted by the language which resembles this --
covariant return types. It has more to do with overriding, though. In
the case where a parent's member function is overridden, a "more
specifically derived" (to coin a phrase?) return type -- that is, a
child of the original return type -- can be specified. To illustrate:

struct A {};
struct B : A {};
struct C { A frobnicate(); };
struct D : C { B frobnicate(); };

Note that you can also have the case where the return types are the
classes themselves, e.g.:

class A { A frob(); };
class B : A { B frob(); };

But apart from this, forget about it.

Luke

Dec 29 '05 #4
On Thu, 29 Dec 2005 01:30:35 -0800, skishorev wrote:
Hi,
Here I am taking two functions. void f(int,int) and another one is
float f(int,int).
Is it possible to overload with return values.

Thx,
kishore


C++ do not allow to overload with return values .. But, there is a trick
which consist to return an object which will invoke the appropriate
function via a type conversion operator.

Just consider the following code as an example...

Gilles Rochefort

#include <iostream>

float foo1(int x,int y)
{
return (float)x/(float)y;
}

int foo2(int x,int y)
{
return (x*y);
}
class resultFoo
{
private:
int _x,_y;

public:
resultFoo(int x,int y):_x(x),_y(y)
{}

operator float()
{ return foo1(_x,_y); }
operator int()
{ return foo2(_x,_y); }

};

resultFoo foo(int x,int y)
{
return resultFoo(x,y);
}

int main(void)
{
float a;
int b;
a = foo(3,4);
b = foo(3,4);
std::cout << "foo(3,4) = " << a << std::endl;
std::cout << "foo(3,4) = " << b << std::endl;

}
Dec 29 '05 #5
On 29 Dec 2005 02:18:03 -0800, "Luke Meyers" <n.***********@gmail.com>
wrote:
Sunil Varma wrote:
No, it's not possible to overload functions based on its return type.


There is one thing permitted by the language which resembles this --
covariant return types. It has more to do with overriding, though. In
the case where a parent's member function is overridden, a "more
specifically derived" (to coin a phrase?) return type -- that is, a
child of the original return type -- can be specified. To illustrate:

struct A {};
struct B : A {};
struct C { A frobnicate(); };
struct D : C { B frobnicate(); };

Note that you can also have the case where the return types are the
classes themselves, e.g.:

class A { A frob(); };
class B : A { B frob(); };


This won't work unless you are returning pointers or references. Even
if it did, you'd have a major problem WRT slicing.

--
Bob Hairgrove
No**********@Home.com
Dec 29 '05 #6
Gilles Rochefort wrote:
On Thu, 29 Dec 2005 01:30:35 -0800, skishorev wrote:
Hi,
Here I am taking two functions. void f(int,int) and another one is
float f(int,int).
Is it possible to overload with return values.

Thx,
kishore


C++ do not allow to overload with return values .. But, there is a trick
which consist to return an object which will invoke the appropriate
function via a type conversion operator.

Just consider the following code as an example...

Gilles Rochefort

#include <iostream>

float foo1(int x,int y)
{
return (float)x/(float)y;
}

int foo2(int x,int y)
{
return (x*y);
}
class resultFoo
{
private:
int _x,_y;

public:
resultFoo(int x,int y):_x(x),_y(y)
{}

operator float()
{ return foo1(_x,_y); }
operator int()
{ return foo2(_x,_y); }

};

resultFoo foo(int x,int y)
{
return resultFoo(x,y);
}

int main(void)
{
float a;
int b;
a = foo(3,4);
b = foo(3,4);
std::cout << "foo(3,4) = " << a << std::endl;
std::cout << "foo(3,4) = " << b << std::endl;

}


Another method is to translate the problem into standard overloading by
passing a dummy parameter. Rather than passing a real object, however,
you can use a trick from _Modern C++ Design_ by creating an object that
contains *only* type information and that can easily be optimized away
by the compiler:

template <typename T>
struct Type2Type
{
typedef T OriginalType;
Type2Type() {}
};

void foo( int i, Type2Type<void> ) { /*...*/ }
int foo( int i, Type2Type<int> ) { /*...*/ return i; }

void bar()
{
foo( 42, Type2Type<void>() );
foo( 42, Type2Type<int>() );
}

This trick is also useful for getting around some template problems on
non-conformant compilers (e.g. VC++ 6).

Cheers! --M

Dec 29 '05 #7

mlimber wrote:
Gilles Rochefort wrote:
On Thu, 29 Dec 2005 01:30:35 -0800, skishorev wrote:
Hi,
Here I am taking two functions. void f(int,int) and another one is
float f(int,int).
Is it possible to overload with return values.

Thx,
kishore


C++ do not allow to overload with return values .. But, there is a trick
which consist to return an object which will invoke the appropriate
function via a type conversion operator.

Just consider the following code as an example...

Gilles Rochefort
#include <iostream>

float foo1(int x,int y)
{
return (float)x/(float)y;
}

int foo2(int x,int y)
{
return (x*y);
}
class resultFoo
{
private:
int _x,_y;

public:
resultFoo(int x,int y):_x(x),_y(y)
{}

operator float()
{ return foo1(_x,_y); }
operator int()
{ return foo2(_x,_y); }

};

resultFoo foo(int x,int y)
{
return resultFoo(x,y);
}

int main(void)
{
float a;
int b;
a = foo(3,4);
b = foo(3,4);
std::cout << "foo(3,4) = " << a << std::endl;
std::cout << "foo(3,4) = " << b << std::endl;

}


Another method is to translate the problem into standard overloading by
passing a dummy parameter. Rather than passing a real object, however,
you can use a trick from _Modern C++ Design_ by creating an object that
contains *only* type information and that can easily be optimized away
by the compiler:

template <typename T>
struct Type2Type
{
typedef T OriginalType;
Type2Type() {}
};

void foo( int i, Type2Type<void> ) { /*...*/ }
int foo( int i, Type2Type<int> ) { /*...*/ return i; }

void bar()
{
foo( 42, Type2Type<void>() );
foo( 42, Type2Type<int>() );
}

This trick is also useful for getting around some template problems on
non-conformant compilers (e.g. VC++ 6).

Cheers! --M

Gilles Rochefort trick is better and express cleanly what you want to
do; above one is a mess; any more suggestions on getting nonstandard
overloading to work?

Dec 29 '05 #8
puzzlecracker wrote:
mlimber wrote:
Gilles Rochefort wrote:
On Thu, 29 Dec 2005 01:30:35 -0800, skishorev wrote:

> Hi,
> Here I am taking two functions. void f(int,int) and another one is
> float f(int,int).
> Is it possible to overload with return values.
>
> Thx,
> kishore

C++ do not allow to overload with return values .. But, there is a trick
which consist to return an object which will invoke the appropriate
function via a type conversion operator.

Just consider the following code as an example...

Gilles Rochefort
#include <iostream>

float foo1(int x,int y)
{
return (float)x/(float)y;
}

int foo2(int x,int y)
{
return (x*y);
}
class resultFoo
{
private:
int _x,_y;

public:
resultFoo(int x,int y):_x(x),_y(y)
{}

operator float()
{ return foo1(_x,_y); }
operator int()
{ return foo2(_x,_y); }

};

resultFoo foo(int x,int y)
{
return resultFoo(x,y);
}

int main(void)
{
float a;
int b;
a = foo(3,4);
b = foo(3,4);
std::cout << "foo(3,4) = " << a << std::endl;
std::cout << "foo(3,4) = " << b << std::endl;

}


Another method is to translate the problem into standard overloading by
passing a dummy parameter. Rather than passing a real object, however,
you can use a trick from _Modern C++ Design_ by creating an object that
contains *only* type information and that can easily be optimized away
by the compiler:

template <typename T>
struct Type2Type
{
typedef T OriginalType;
Type2Type() {}
};

void foo( int i, Type2Type<void> ) { /*...*/ }
int foo( int i, Type2Type<int> ) { /*...*/ return i; }

void bar()
{
foo( 42, Type2Type<void>() );
foo( 42, Type2Type<int>() );
}

This trick is also useful for getting around some template problems on
non-conformant compilers (e.g. VC++ 6).

Cheers! --M

Gilles Rochefort trick is better and express cleanly what you want to
do; above one is a mess;

[snip]

"Better" is a function of need, and the OP's code may be greatly
simplified from the actual code. In any case, the Type2Type trick is
useful in templatized code like this (drawn from section 9.2 of _Modern
C++ Design_):

template <class T>
class AbstractFactoryUnit
{
virtual T* DoCreate( Type2Type<T> ) = 0;
virtual ~AbstractFactoryUnit() {}
};

In the AbstractFactory class presented in the book, there can be
multiple DoCreate overloads within the same class hierarchy, and the
Type2Type trick is used to disambiguate them. The other trick above
could not do so.

Cheers! --M

Dec 29 '05 #9
puzzlecracker wrote:
mlimber wrote:
Gilles Rochefort wrote:
On Thu, 29 Dec 2005 01:30:35 -0800, skishorev wrote:

> Hi,
> Here I am taking two functions. void f(int,int) and another one is
> float f(int,int).
> Is it possible to overload with return values.
>
> Thx,
> kishore

C++ do not allow to overload with return values .. But, there is a trick
which consist to return an object which will invoke the appropriate
function via a type conversion operator.

Just consider the following code as an example...

Gilles Rochefort
#include <iostream>

float foo1(int x,int y)
{
return (float)x/(float)y;
}

int foo2(int x,int y)
{
return (x*y);
}
class resultFoo
{
private:
int _x,_y;

public:
resultFoo(int x,int y):_x(x),_y(y)
{}

operator float()
{ return foo1(_x,_y); }
operator int()
{ return foo2(_x,_y); }

};

resultFoo foo(int x,int y)
{
return resultFoo(x,y);
}

int main(void)
{
float a;
int b;
a = foo(3,4);
b = foo(3,4);
std::cout << "foo(3,4) = " << a << std::endl;
std::cout << "foo(3,4) = " << b << std::endl;

}
Another method is to translate the problem into standard overloading by
passing a dummy parameter. Rather than passing a real object, however,
you can use a trick from _Modern C++ Design_ by creating an object that
contains *only* type information and that can easily be optimized away
by the compiler:

template <typename T>
struct Type2Type
{
typedef T OriginalType;
Type2Type() {}
};

void foo( int i, Type2Type<void> ) { /*...*/ }
int foo( int i, Type2Type<int> ) { /*...*/ return i; }

void bar()
{
foo( 42, Type2Type<void>() );
foo( 42, Type2Type<int>() );
}

This trick is also useful for getting around some template problems on
non-conformant compilers (e.g. VC++ 6).

Cheers! --M


[snip] any more suggestions on getting nonstandard
overloading to work?


You could use template specialization (but not on VC6):

template <class T> T Foo();

template<> int Foo()
{
return 42;
}

template<> double Foo()
{
return 42.0;
}

int main()
{
const double d = Foo<double>();
return Foo<int>();
}

Cheers! --M

Dec 29 '05 #10
g
you must use tuples,take a look at Boost.

Dec 29 '05 #11
g wrote:
you must use tuples,take a look at Boost.


Must use them for what?

Cheers! --M

Dec 29 '05 #12
g
yes!
why to create dummy classes at your own for the returns????
there is tuple for this;-)!
g.

Dec 29 '05 #13
g wrote:
yes!
Yes what?
why to create dummy classes at your own for the returns????
there is tuple for this;-)!


First, I did not create a dummy class at all (unless you mean in my
first response about Loki::Type2Type, in which case you responded to
the wrong post and didn't quote enough -- let me rephrase that -- ANY
context to clarify your meaning).

Second, please demonstrate your solution with a snippet of code.

Cheers! --M

Dec 29 '05 #14
Bob Hairgrove wrote:
class A { A frob(); };
class B : A { B frob(); };


This won't work unless you are returning pointers or references. Even
if it did, you'd have a major problem WRT slicing.


Gah, of course you're right. It's a good thing we've got compilers to
tell us these things, too. :)

Luke

Dec 29 '05 #15

mlimber wrote:
puzzlecracker wrote:
mlimber wrote:
Gilles Rochefort wrote:
> On Thu, 29 Dec 2005 01:30:35 -0800, skishorev wrote:
>
> > Hi,
> > Here I am taking two functions. void f(int,int) and another one is
> > float f(int,int).
> > Is it possible to overload with return values.
> >
> > Thx,
> > kishore
>
> C++ do not allow to overload with return values .. But, there is a trick
> which consist to return an object which will invoke the appropriate
> function via a type conversion operator.
>
> Just consider the following code as an example...
>
> Gilles Rochefort

>
> #include <iostream>
>
> float foo1(int x,int y)
> {
> return (float)x/(float)y;
> }
>
> int foo2(int x,int y)
> {
> return (x*y);
> }
>
>
> class resultFoo
> {
> private:
> int _x,_y;
>
> public:
> resultFoo(int x,int y):_x(x),_y(y)
> {}
>
> operator float()
> { return foo1(_x,_y); }
>
>
> operator int()
> { return foo2(_x,_y); }
>
> };
>
> resultFoo foo(int x,int y)
> {
> return resultFoo(x,y);
> }
>
> int main(void)
> {
> float a;
> int b;
> a = foo(3,4);
> b = foo(3,4);
> std::cout << "foo(3,4) = " << a << std::endl;
> std::cout << "foo(3,4) = " << b << std::endl;
>
> }

Another method is to translate the problem into standard overloading by
passing a dummy parameter. Rather than passing a real object, however,
you can use a trick from _Modern C++ Design_ by creating an object that
contains *only* type information and that can easily be optimized away
by the compiler:

template <typename T>
struct Type2Type
{
typedef T OriginalType;
Type2Type() {}
};

void foo( int i, Type2Type<void> ) { /*...*/ }
int foo( int i, Type2Type<int> ) { /*...*/ return i; }

void bar()
{
foo( 42, Type2Type<void>() );
foo( 42, Type2Type<int>() );
}

This trick is also useful for getting around some template problems on
non-conformant compilers (e.g. VC++ 6).

Cheers! --M


[snip]
any more suggestions on getting nonstandard
overloading to work?


You could use template specialization (but not on VC6):

template <class T> T Foo();

template<> int Foo()
{
return 42;
}

template<> double Foo()
{
return 42.0;
}

int main()
{
const double d = Foo<double>();
return Foo<int>();
}

Cheers! --M

you can NOT have functions that use template specializations:
according to the standard, only member functions and types can be
specialized.

Dec 29 '05 #16
g

Ο/Η mlimber *γραψε:
g wrote:
yes!


Yes what?
why to create dummy classes at your own for the returns????
there is tuple for this;-)!


First, I did not create a dummy class at all (unless you mean in my
first response about Loki::Type2Type, in which case you responded to
the wrong post and didn't quote enough -- let me rephrase that -- ANY
context to clarify your meaning).

Second, please demonstrate your solution with a snippet of code.

when you want to be able to return from a function multiple values,if
there is a condition and if it is true to return an int else return
something else then instead of overloading & etc
use tuples
go at boost site and search for tuples!

Dec 30 '05 #17

g wrote:
Ο/Η mlimber *γραψε:
g wrote:
yes!


Yes what?
why to create dummy classes at your own for the returns????
there is tuple for this;-)!


First, I did not create a dummy class at all (unless you mean in my
first response about Loki::Type2Type, in which case you responded to
the wrong post and didn't quote enough -- let me rephrase that -- ANY
context to clarify your meaning).

Second, please demonstrate your solution with a snippet of code.

when you want to be able to return from a function multiple values,if
there is a condition and if it is true to return an int else return
something else then instead of overloading & etc
use tuples
go at boost site and search for tuples!


But the question was how to overload for multiple types, not multiple
values. For this purpose, boost::any or boost::variant might help, but
boost::tuple seems to be misapplied here.

Cheers! --M

Dec 30 '05 #18
puzzlecracker wrote:
mlimber wrote:
puzzlecracker wrote:
mlimber wrote:
> Gilles Rochefort wrote:
> > On Thu, 29 Dec 2005 01:30:35 -0800, skishorev wrote:
> >
> > > Hi,
> > > Here I am taking two functions. void f(int,int) and another one is
> > > float f(int,int).
> > > Is it possible to overload with return values.
> > >
> > > Thx,
> > > kishore
> >
> > C++ do not allow to overload with return values .. But, there is a trick
> > which consist to return an object which will invoke the appropriate
> > function via a type conversion operator.
> >
> > Just consider the following code as an example...
> >
> > Gilles Rochefort

> >
> > #include <iostream>
> >
> > float foo1(int x,int y)
> > {
> > return (float)x/(float)y;
> > }
> >
> > int foo2(int x,int y)
> > {
> > return (x*y);
> > }
> >
> >
> > class resultFoo
> > {
> > private:
> > int _x,_y;
> >
> > public:
> > resultFoo(int x,int y):_x(x),_y(y)
> > {}
> >
> > operator float()
> > { return foo1(_x,_y); }
> >
> >
> > operator int()
> > { return foo2(_x,_y); }
> >
> > };
> >
> > resultFoo foo(int x,int y)
> > {
> > return resultFoo(x,y);
> > }
> >
> > int main(void)
> > {
> > float a;
> > int b;
> > a = foo(3,4);
> > b = foo(3,4);
> > std::cout << "foo(3,4) = " << a << std::endl;
> > std::cout << "foo(3,4) = " << b << std::endl;
> >
> > }
>
> Another method is to translate the problem into standard overloading by
> passing a dummy parameter. Rather than passing a real object, however,
> you can use a trick from _Modern C++ Design_ by creating an object that
> contains *only* type information and that can easily be optimized away
> by the compiler:
>
> template <typename T>
> struct Type2Type
> {
> typedef T OriginalType;
> Type2Type() {}
> };
>
> void foo( int i, Type2Type<void> ) { /*...*/ }
> int foo( int i, Type2Type<int> ) { /*...*/ return i; }
>
> void bar()
> {
> foo( 42, Type2Type<void>() );
> foo( 42, Type2Type<int>() );
> }
>
> This trick is also useful for getting around some template problems on
> non-conformant compilers (e.g. VC++ 6).
>
> Cheers! --M

[snip]
any more suggestions on getting nonstandard
overloading to work?


You could use template specialization (but not on VC6):

template <class T> T Foo();

template<> int Foo()
{
return 42;
}

template<> double Foo()
{
return 42.0;
}

int main()
{
const double d = Foo<double>();
return Foo<int>();
}

Cheers! --M

you can NOT have functions that use template specializations:
according to the standard, only member functions and types can be
specialized.


Not only that, but the template arg MUST be used in the function
argument list (not just the return type). What sort of bonehead would
make such a foolish suggestion?! (In my defense, that snippet of code
compiled fine with Comeau. :-) )

Cheers! --M

Dec 30 '05 #19

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

Similar topics

7
by: Piotre Ugrumov | last post by:
I have tried to implement the overload of these 2 operators. ostream & operator<<(ostream &out, Person &p){ out<<p.getName()<<" "<<p.getSurname()<<", "<<p.getDateOfBirth()<<endl; return out; }...
1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
14
by: Lance | last post by:
That is the question. In an unmanaged C++ DLL I'm making calls to, one of the function prototypes is ///// GM_DLL_EXPORTED GM_Error_t32 __stdcall GM_DrawLayerList ( HDC aDC, // Device...
3
by: sebastien.lannez | last post by:
Thanks for your quick response. Using Python as an algebraic parser for symbolic mathematical equation and I need that the 'in' operator returns an object based on its two arguments.
3
by: i3x171um | last post by:
To start off, I'm using GCC4. Specifically, the MingW (setjmp/longjmp) build of GCC 4.2.1 on Windows XP x64. I'm writing a class that abstracts a message, which can be either an integer (stored as...
2
by: DanYan | last post by:
So I was doing some stuff in Javascript, and I want to get access to a function's scope chain. As a simplified example of what I actually am trying to do, suppose I have this: function add(b)...
1
by: fabian.lim | last post by:
Hi all, Im having a problem with my code. Im programming a vector class, and am trying to overload the () operator in 2 different situations. The first situation is to assign values, e.g. Y =...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.