Sign in to follow this  

can I overload return types?

This topic is 3290 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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(); }

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3290 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this