Sorry I missed this one.
True: The language does not allow does not allow you to directly overload on return type:
1 2 3 4 5
|
struct dumbonium
{
int operator () const { return -7; }
char operator () const { return 'A'; } // ERROR!
};
|
However, it is entirely possible to
cheat. Yup. My favorite, non-
Boost.Any way to do it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
struct awsomium
{
operator int () const { return -7; }
operator char () const { return 'A'; }
operator double () const { return 3.141592; }
};
int main()
{
awsomium a;
int x = a;
char c = a;
std::cout << x << ", " << c << ", " << (double)a << "\n";
}
|
The major caveat is that you must be explicit about the return type you desire, otherwise type promotion rules will kick in and confuse you.
As an aside, the most useful reason to do this kind of thing is for "returning" a container from a function. For example, suppose you have a fancy "split" function, which takes a string and converts it into a list of strings.
Note -- this code is fairly simplified to keep other confusing things out of the discussion.
1 2 3 4 5 6 7 8 9 10
|
template <typename Container>
Container&
split(
Container& result,
const std::string& s,
const std::string& delimiters
) {
...
return result;
}
|
Used thus:
1 2 3
|
string s = "Hello world!";
vector <string> words;
split( words, s, " " );
|
What we'd
like to say is something like this:
1 2
|
string s = "Hello world!";
vector <string> words = split( s, " " );
|
For that, we'll need two helpers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
// A struct that overloads on "return" type
stuct split_helper
{
const std::string& s;
const std::string& delimiters;
split_helper( const std::string& s, const std::string& delimiters ):
s( s ), delimiters( delimiters )
{ }
template <typename Container>
operator Container () const
{
Container result;
return split( result, s, delimiters );
}
};
// A make-functor
inline
split_helper
split(
const std::string& s,
const std::string& delimiters
) {
return split_helper( s, delimiters );
}
|
Voila!
The method still has issues, of course -- careless use can confuse your compiler.
Also useful are the
<type_traits> library and the SFINAE principle.
Hope this helps.