can I overload return types?

Started by
7 comments, last by DevFred 15 years, 4 months ago
I figured this was valid but I can't seem to find what I'm looking for maybe I'm searching all the wrong way. Is it possible to overload the return type of a function similar to that of the arguments? ie;

float Get_Data();
int   Get_Data();
or is it just plain impossible or too cumbersome?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Advertisement
Quote:Is it possible to overload the return type of a function similar to that of the arguments?


No. The compiler has no way of determining which function you want.

Technically I should be asking what language you're using, but for now I'm assuming some form of C based language.
As noted above, overloading the return type of a function is generally not allowed in most scenarios in programming languages. I just wanted to point out that, in object-oriented programming, some languages support covariant return types. Useful, but unfortunately not the functionality you're looking for!
The best you can do for something like that is
void GetData(float&);
void GetData(int&);
void GetData(std::string&);
etc.
Quote:Original post by freeworld
Is it possible to overload the return type of a function similar to that of the arguments?



Not in almost every language. And even those that do can't treat it exactly like parameter overloading because using the return type is harder/different to disambiguate.
In c++ you can parametrize a single method's return type. This type of behaviour is *not* regarded as overloading because you cannot define (or specialize) the set of overload types that you want to be available and it instead works generically (parametrically) for only one function. Additionally the appropriate function type will not be infered when used for instance in an assignment statement. You must state it up front ( eg get< type>() ). That said its kind of useful when making accessors or wrapping things like boost variant or boost any.

struct A
{
template< class T>
T get()
{
return (T )0;
}
};

int main()
{
A a;
int ix = a.get< int>();
float fx = a.get< float>();
std::cout << "ix " << ix << " fx " << fx << std::endl;
}
Expanding on the template direction, and with template specialization:

template<typename T> T     foobar       () { assert(false); }template<>           int   foobar<int>  () { return 10; }template<>           float foobar<float>() { return 3.14f; }


An alternate way which doesn't require you to specify the type when calling the function, but still needs the type conversion required to be unambiguous:

struct closure{  operator int()   const { return 10; }  operator float() const { return 3.14f; }};closure foobar() { return closure(); }
Quote:Original post by KulSeran
The best you can do for something like that is
void GetData(float&);
void GetData(int&);
void GetData(std::string&);
etc.


don't no why I didn't think of that... I'll have to try it this way and see if it makes sense with what I was trying to accomplish.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Why does it have to be overloaded? Couldn't you just use
float get_float_data();int   get_int_data();

?

This topic is closed to new replies.

Advertisement