mercredi 7 octobre 2020

How to rewrite with boost::optional in C++11?

How do I rewrite the following code to use boost::optional or boost::none, in C++11?

std::unique_ptr<FooBase> find( std::string key)
{
    std::map<std::string, std::function<std::unique_ptr<FooBase>(void)> > m{
                       {"key1", [](){return std::make_unique<BarDerived>();} },
                       {"key2", [](){return std::make_unique<BarDerived1>();} } };
                                        
    auto it = m.find(key);
    if (it != std::end(m))
        return (it->second());  
    else 
        return nullptr;                                        

}

Aucun commentaire:

Enregistrer un commentaire