Polymorphism involving objects with value semantics (C++)

> Coding, hacking, computer graphics, game dev, and such...
User avatar
fips
Site Admin
Posts: 170
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague
Contact:

Polymorphism involving objects with value semantics (C++)

Post by fips »

A few months ago, I really enjoyed two very interesting C++ talks (1) (2) given by a very clever guy named Sean Parent. I was immediately enchanted by his approach to polymorphism involving objects with value semantics, which makes virtual dispatch and dynamic memory allocation just an implementation detail rather than a primary means of writing client code. I had been interested in this topic for some time but never encountered such a good source of inspiration, so I've decided to give it a try and come up with this minimal experiment to help me study this approach:

VIEW THE CODE BELOW IN FULL-SCREEN (polymorphism_value_semantics_sample.cpp)

Code: Select all

/*
(c) 2013 +++ Filip Stoklas, aka FipS, http://www.4FipS.com +++
THIS CODE IS FREE - LICENSED UNDER THE MIT LICENSE
ARTICLE URL: http://forums.4fips.com/viewtopic.php?f=3&t=1194
*/

#include <iostream>
#include <vector>
#include <memory>

struct Circle
{
    void draw(std::ostream &out) { out << "Circle::draw()\n"; }
};

struct Square
{
    void draw(std::ostream &out) { out << "Square::draw()\n"; }
};

struct List
{
    template <typename Shape_T>
    void push(Shape_T shape)
    {
        _shapes.emplace_back(If_ptr(new Slot<Shape_T>(std::move(shape))));
    }

    void draw(std::ostream &out)
    {
        for(auto &shape : _shapes)
        {
            shape->draw(out);
        }
    }

 private:

    struct If
    {
        virtual ~If() {}
        virtual void draw(std::ostream &out) = 0;
    };

    template <typename Shape_T>
    struct Slot : public If
    {
        virtual void draw(std::ostream &out) { shape.draw(out); }

        Slot(Shape_T shape) : shape(shape) {}
        Shape_T shape;
    };

    typedef std::unique_ptr<If> If_ptr;
    std::vector<If_ptr> _shapes;
};

int main()
{
    Square s;
    Circle c;
    List l;

    l.push(s);
    l.push(c);
    l.push(c);
    l.push(c);

    l.draw(std::cout);

    return 0;
}

// Compiled under Visual C++ 2012, output:
// Square::draw()
// Circle::draw()
// Circle::draw()
// Circle::draw()
Follow the discussion on Reddit...