You cannot overload a function based on the return type. You can force your operator to return an int and string variant, and let the user verify that it was indeed returned, but it is cumbersome. If the type of the return value can be determined at compile time, you can implement operator overloads, bearing in mind the different types of indexes. Something like that:
struct as_string { as_string( std::size_t index ) : _index( index ){} std::size_t const _index; }; ... int operator[]( int index ) const { ... }; std::string operator[]( as_string const& index ) const { ... };
And then the caller called object[0] to get the result of int , or object[as_string(0)] to get the result of string .
source share