I have a class that looks like this:
class Container {
public:
Container(){
Doubles["pi"] = 3.1415;
Doubles["e"] = 2.7182;
Integers["one"] = 1;
Integers["two"] = 2;
}
// Bracket.cpp:23:9: error: 'auto' return without trailing return type
// auto& operator[](const std::string&);
auto& operator[](const std::string& key);
private:
std::map<std::string, double> Doubles;
std::map<std::string, int> Integers;
};
I'd like to overload the operator[]
function to return something from either Doubles
or Integers
depending on a key that is passed. However, I don't know a prioi if what will be returned is a double
or a int
. I'd like to implement the operator[]
function in this way:
// Compiler error
// Bracket.cpp:30:1: error: 'auto' return without trailing return type
// auto& Container::operator[](const std::string& key){
auto& Container::operator[](const std::string& key){
std::cout << "I'm returning the value associated with key: "
<< key << std::endl;
auto D_search = Doubles.find(key);
if (D_search != Doubles.end()){
std::cout << "I found my key in Doubles with value: " <<
D_search->second << std::endl;
return D_search->second;
}
else{
auto I_search = Integers.find(key);
if (I_search != Integers.end()){
std::cout << "I found my key in Integers with value: " <<
I_search->second << std::endl;
return I_search->second;
}
else{
std::cout << "I didn't find a value for the key." << std::endl;
}
}
}
Is there a way to create a single operator[]
function to return multiple types?
This was driven with this simple code:
int main(){
Container Bucket;
double pi(Bucket["pi"]);
std::cout << "The value of pi is: " << pi << std::endl;
return 0;
}
Best Answer
The C++11 version of
auto
as return type only allows you to postpone the return type declaration until after the function parameters have been declared:Here you can't write
decltype(x + y) add(...)
because the compiler doesn't know whatx
andy
are.The C++14 version of
auto
permits return type deduction by the compiler. It tells the compiler to deduce the return type of the function based on the body, but it's still a single return type, so it still does not do what you want.