Reputation: 30885
I have this function for finding key in map and return the right value with type:
template< typename T >
T Foo::findInMap(std::string key) {
std::map<std::string, std::any>::iterator it = options.find(key);
if (it != options.end())
{
return std::any_cast<T>(it->second);
}
return nullptr;
}
the map looks like this:
std::map<std::string, std::any> options;
How can I call and modify the function to get the best out of C++17?
Upvotes: 1
Views: 94
Reputation: 11
any_cast(T&) throws std::bad_any_cast, use any_cast(const T*) instead
struct Test {
template <typename T>
std::optional<T> findInMap(std::string key) {
if (auto it = options.find(key); it != options.end()) {
if(auto ptr = std::any_cast<T>(&it->second); ptr) {
return std::make_optional<T>(*ptr);
}
}
return std::nullopt;
}
std::map<std::string, std::any> options;
};
Upvotes: 0
Reputation: 10238
For simultaneous declaration and initialization, std::optional may offer the superior approach. But type variability and optionality take their toll on context (read: invocation and case distinctions) in any case. I decided to take the cost of loading the surrounding scope with a variable that is default-initialized to be overwritten afterwards in the found case. This way I get a function call whose argument determines the type, making the type specification in angle brackets unnecessary for the caller.
I also renamed the function into map_assign
(removed the member function status since irrelevant here):
template< typename T >
bool map_assign(const std::string& key, T* value) {
auto it = options.find(key);
if (it != options.end())
{
*value = std::any_cast<T>(it->second);
return true;
}
return false;
}
You can call it like this:
std::string value;
if (map_assign("somekey", &value)) {
// found case
} else {
// not-found case
}
Upvotes: 3
Reputation: 29965
Returning a nullptr
is not always possible. Modern C++ way of returning optional values is to use std::optional
. You can use auto
instead of the long iterator type name. Here's one way you can do it:
template< typename T >
std::optional<T> findInMap(std::string key) {
auto const it = options.find(key);
if (it != options.end())
return std::optional<T>{ std::any_cast<T>(it->second) };
return std::nullopt;
}
Upvotes: 6