Extract from typelist by runtime value I combined the ideas from this answer and a comment to this question to create a new instance of a type extracted from a type list. The index of the desired type within the type list is provided at run time, only.

#include &lt;iostream&gt;
#include &lt;memory&gt;

class Base
{
public:
    virtual void foo() = 0;
};

class A : public Base
{
public:
    void foo() override
    {
        std::cout &lt;&lt; "A" &lt;&lt; std::endl;
    }
};

class B : public Base
{
public:
    void foo() override
    {
        std::cout &lt;&lt; "B" &lt;&lt; std::endl;
    }
};

template &lt;class... Types&gt;
class type_list {};

using list_t = type_list&lt;A,B&gt;;

template &lt;class... Types&gt;
static constexpr std::size_t length = sizeof...(Types);

template &lt;std::size_t idx, class... Types&gt;
class extract
{
    static_assert(idx &lt; sizeof...(Types), "index out of bounds");

    template &lt;std::size_t i, std::size_t n, class... Rest&gt;
    struct extract_impl;

    template &lt;std::size_t i, std::size_t n, class T, class... Rest&gt;
    struct extract_impl&lt;i, n, T, Rest...&gt;
    {
        using type = typename extract_impl&lt;i + 1, n, Rest...&gt;::type;
    };

    template &lt;std::size_t n, class T, class... Rest&gt;
    struct extract_impl&lt;n, n, T, Rest...&gt;
    {
        using type = T;
    };

public:
    using type = typename extract_impl&lt;0, idx, Types...&gt;::type;
};

template &lt;std::size_t idx, class TypeList&gt;
struct type_list_extract;

template &lt;std::size_t idx, template &lt;class...&gt; class TypeList, class... Types&gt;
struct type_list_extract&lt;idx, TypeList&lt;Types...&gt;&gt;
{
    using type = typename extract&lt;idx, Types...&gt;::type;
};

template &lt;std::size_t idx, class TypeList&gt;
using type_list_extract_t = typename type_list_extract&lt;idx, TypeList&gt;::type;

template&lt;std::size_t idx, bool done = false, class... Types&gt;
struct action_wrapper
{
    static std::unique_ptr&lt;Base&gt; get_instance_by_id(std::size_t id)
    {
        if (id == idx)
        {
            using type = type_list_extract_t&lt;idx, Types...&gt;;
            return std::make_unique&lt;type&gt;();
        }

        static constexpr auto cont = length&lt;Types...&gt; &lt; idx + 1;
        return action_wrapper&lt;idx + 1, cont, Types...&gt;::get_instance_by_id(id);
    }
};

template&lt;std::size_t idx, class... Types&gt;
struct action_wrapper&lt;idx, true, Types...&gt;
{
    static std::unique_ptr&lt;Base&gt; get_instance_by_id(std::size_t id)
    {
        return nullptr;
    }
};

static std::unique_ptr&lt;Base&gt; get_instance_by_id(std::size_t id)
{
    return action_wrapper&lt;0, false, list_t&gt;::get_instance_by_id(id);
}

int main()
{
    std::unique_ptr&lt;Base&gt; ptr = get_instance_by_id(1);
    if (ptr)
    {
        ptr-&gt;foo();
    }
}
Prefer using \n over std::endl



Other than that, it looks like your code is rather clean and up-to-date style-wise.  I might suggest that the get_instance_by_id return the actual type, not the Base in case it gets used in a context where a specific one is known at compile time (or within other template code).