Creating a class object just based on the class type I am trying to use a map as a way to implement an object factory. In Python it is possible to store a class type in a map, and use it later to create objects from that type:

class Foo:
    # ...

class Bar:
    # ...

factory_map = { 'foo' : Foo, 'bar' : Bar }
foo_object = factory_map['foo']()


I am not aware of a similar feature (i.e., storing a type in a map) in C++, so I came up with a solution based on lambda functions where the map stores functions responsible to create the objects:

class Base {
public:
    typedef std::unique_ptr&lt;Base&gt; Pointer;
    virtual void hi() = 0;
};

class Foo : public Base {
public:
    void hi() { std::cout &lt;&lt; "Hi, it's foo\n"; }
};

class Bar : public Base {
public:
    void hi() { std::cout &lt;&lt; "Hi, it's bar\n"; }
};

int main() {
    std::map&lt;
        std::string,
        std::function&lt;Base::Pointer()&gt;&gt; factory_map = {
            { "foo", []() { return Base::Pointer(new Foo()); } },
            { "bar", []() { return Base::Pointer(new Bar()); } },
        };

    Base::Pointer b1 = factory_map["foo"]();
    b1-&gt;hi();

    Base::Pointer b2 = factory_map["bar"]();
    b2-&gt;hi();
}


This might be the most straightforward solution to obtain Python's simplicity. I am wondering, however, if the usage of lambdas is the best way to create the factory. Specifically, I would like to know what are the pros/cons of this approach compared to a more "traditional" factory design (i.e., one using if/else conditionals to decide the type of the object to create).
The common argument for preferring one over the other for a dictionary (map) look up and a conditional (switch) look up is performance over maintainability.

The dictionary look up usually makes it easier to separate concerns. It also makes the code more concise and arguably more readable when invoking the factory functions.

The conditional look up avoids traversing a map and any types of indirection allowing for arguably faster access to object creation. It also avoids the overhead of creating the map/registering functions in the map.

There are also arguments for locality of reference vs. dependency management where the switch statement may keep object creation local to the call site, while the map approach passes off creation to a function or object that knows how to better handle the problem.

These are the typical arguments that I see for each of those different approaches to this problem.