I have a map that allows std::any as values, then I return the std::any object.
I would like to save some characters in my code. so I have
class MyMap {
std::map<std::string, std::any> map;
public:
template<typename T>
const T & operator[](const std::string & name) const {
auto & aux = map[name];
return std::any_cast<T&>(aux);
}
}
so, instead of
auto foo = std::any_cast<int>(myMap["key"]);
I would like to
auto foo = myMap<int>["key"]; // or something like this, beacuse, the compiler tells this syntax is incorrect
I don't know if this is even possible, and if is, how do I have to invoke the operator[]?
question from:https://stackoverflow.com/questions/65517915/how-to-invoke-a-template-operator-in-c