Interface-based polymorphic collection Here is a small C++11 utility collection to store any object that satisfies a given interface. Only the basic concepts are provided, I did not try to create a full collection:

#include &lt;memory&gt;
#include &lt;utility&gt;
#include &lt;vector&gt;
#include &lt;boost/iterator/indirect_iterator.hpp&gt;

template &lt;typename Interface, template &lt;typename&gt; class Adapter&gt;
class poly_collection
{
public:
    using iterator = boost::indirect_iterator&lt;typename std::vector&lt;std::unique_ptr&lt;Interface&gt;&gt;::iterator&gt;;

    template&lt;typename T, typename... Args&gt;
    auto push(Args&amp;&amp;... args)
        -&gt; void
    {
        auto ptr = new Adapter&lt;T&gt;{ std::forward&lt;Args&gt;(args)... };
        entities.emplace_back(ptr);
    }

    template&lt;typename T&gt;
    auto push(T&amp;&amp; other)
        -&gt; void
    {
        auto ptr = new Adapter&lt;T&gt;{ std::forward&lt;T&gt;(other) };
        entities.emplace_back(ptr);
    }

    auto begin()
        -&gt; iterator
    {
        return { std::begin(entities) };
    }

    auto end()
        -&gt; iterator
    {
        return { std::end(entities) };
    }

private:
    std::vector&lt;std::unique_ptr&lt;Interface&gt;&gt; entities;
};


Here is an example of use. Imagine you have an abstract class named Shape. For the sake of simplicity, it will only have one abstract method, draw:

struct Shape
{
    virtual void draw() const = 0;

    virtual ~Shape() {};
};


Now, you have some nice classes, Rectangle and Circle, that you didn't write but that satisfy the Shape interface:

struct Circle
{
    Circle(int x, int y, int radius) {}

    void draw() const
    {
        std::cout &lt;&lt; "Circle\n";
    }
};

struct Rectangle
{
    Rectangle(int x, int y, int height, int width) {}

    void draw() const
    {
        std::cout &lt;&lt; "Rectangle\n";
    }
};


You would like to store Rectangle and Circle instances in a colleciton as if they inherited from a common base class. With our solution, you only have to write a new class: ShapeAdapter, and then feed it to the poly_collection:

template&lt;typename T&gt;
struct ShapeAdapter:
    Shape
{
    template&lt;typename... Args&gt;
    ShapeAdapter(Args&amp;&amp;... args):
        data(std::forward&lt;Args&gt;(args)...)
    {}

    virtual void draw() const override
    {
        data.draw();
    }

    T data;
};

int main()
{
    poly_collection&lt;Shape, ShapeAdapter&gt; shapes;

    shapes.push&lt;Circle&gt;(1, 2, 3);
    shapes.push&lt;Rectangle&gt;(4, 5, 6, 7);

    Circle c = { 8, 9, 10 };
    shapes.push(c);

    for (auto&amp;&amp; shape: shapes)
    {
        shape.draw();
    }
}


So, what do you think of the idea? Would that be an interesting utility? Is there any way I could improve such a collection appart from adding some functions to mimick the standard library's collections ones?
As usual with your code, I can only come up with very minor nitpicks.

With both push functions, there is a small chance of a memory leak. After the construction of a new Adapter&lt;T&gt; using:

auto ptr = new Adapter&lt;T&gt;{ std::forward&lt;Args&gt;(args)... };


If entities.emplace_back(ptr); fails (say if relocation is required, the vector needs to expand, and this fails), then ptr will leak. Instead of doing it this way, I'd suggest using make_unique and moving the unique_ptr into entities using push_back instead:

template&lt;typename T, typename... Args&gt;
auto push(Args&amp;&amp;... args)
    -&gt; void
{
    auto ptr = std::make_unique&lt;Adapter&lt;T&gt;&gt;(std::forward&lt;Args&gt;(args)...);
    entities.push_back(std::move(ptr));
}