Send type into function using templates

北战南征 提交于 2021-02-11 06:29:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!