Simple factory retrieving object by name Despite the fact that this code works, I would like to know if there's any situation I did not anticipated, and/or if what I am trying to do is some what wrong.

-std=C++11 flag needed

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;map&gt;
#include &lt;typeinfo&gt;
#include &lt;functional&gt;

class object
{
    public:

    virtual ~object(){}

    virtual std::string to_string() = 0;
};

class SomeObject : public object
{
    public:

    SomeObject(){}
    virtual ~SomeObject(){}

    virtual std::string to_string()
    {
        return "I am a type of SomeObject";
    } 
};

class SomeOtherObject : public object
{
    public:

    SomeOtherObject(){}
    virtual ~SomeOtherObject(){}

    virtual std::string to_string()
    {
        return "I am a type of SomeOtherObject";
    } 
};

std::map&lt;std::string, std::function&lt;object*()&gt; &gt; types;

template&lt;typename O&gt;
inline
static void register_type(const std::string &amp; name)
{
    types[name] = [](){ return new O; };
}

static object * get_object(const std::string &amp; object_name )
{
    return types[object_name]();
}

int main()
{
    register_type&lt;SomeObject&gt;("SomeObject");
    register_type&lt;SomeOtherObject&gt;("SomeOtherObject");

    object * some = get_object("SomeObject");
    object * some_other = get_object("SomeOtherObject");

    std::cout &lt;&lt; "::" &lt;&lt; some-&gt;to_string() &lt;&lt; std::endl;
    std::cout &lt;&lt; "::" &lt;&lt; some_other-&gt;to_string() &lt;&lt; std::endl;

    delete some;
    delete some_other;

    std::cout &lt;&lt; "exit" &lt;&lt; std::endl;

    return 0;
}
In general your code looks fine, though there are some points to improve:

Memory management

Instead of using raw pointers and new delete, you should use a smart pointer to express transfer of ownership explicitly:

template&lt;typename O&gt;
void register_type(const std::string &amp; name) {
    types[name] = [](){ return std::unique_ptr&lt;O&gt;(new O); };
}

std::unique_ptr&lt;object&gt; get_object(const std::string &amp; object_name ) {
    return types[object_name]();
}


Thus it's clear, the caller will own the object instance, and doesn't need to struggle calling delete correctly if the obtained instance goes out of scope.

Template parameter naming

The more usual name for the template parameter is rather T than O

Use a factory class instead of global functions and global map

Consolidate the code of the register_type(), get_object() functions and the types map variable into a class Registry, and provide a single instance of that one.

This would allow to encapsulate the types map as a private class member.

You could consider making that class a Singleton.

Check if the type name is registered

You should check if the string passed to the get_object() function was already registered in the map, rather than blindly calling the default (NOP) function from the map.

Usage of static for a global function

You should be aware that static actually make the register_type() function only visible for the translation unit, it appears in. Actually you should not need it.



BTW, the technique you're using is often seen for unit testing frameworks (like e.g. google-test) as test case factories, also often coming along with stringizing macros for the registered classes.