问题
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?
回答1:
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;
}
回答2:
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
}
来源:https://stackoverflow.com/questions/66041415/send-type-into-function-using-templates