# can I overload return types?

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

## 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 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 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 on other sites
The best you can do for something like that is
void GetData(float&);
void GetData(int&);
void GetData(std::string&);
etc.

##### Share on other sites
Quote:
 Original post by freeworldIs 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 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 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 on other sites
Quote:
 Original post by KulSeranThe best you can do for something like that isvoid 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 on other sites
Why does it have to be overloaded? Couldn't you just use
float get_float_data();int   get_int_data();

?