lundi 24 avril 2017

Non-copying std::shared_ptr

I store "instances of different types" with "shared ownership". That's what I currently do:

class Destructible {
public:
    virtual ~Destructible() = default;
};

// UGLY
class MyType1 : public Destructible { ... };
class MyTypeN : public Destructible { ... };

class Storage {
    std::vector<std::shared_ptr<Destructible>> objects_;
    ...
}

I'd love to switch to boost::any, removing all these conformances and gaining the ability to store instances of truly any type. Also I like boost::any interface and boost::any_cast.

But my types don't satisfy ValueType requirements, they are not copyable. What is the best (preferably existing) solution for this problem? Something like shared_any_ptr, which captures destructor at creation, has type erasure, reference counter and can do any_cast.

Edit: boost::any allows creation with move, but I'd prefer not to even move and use pointers.

Aucun commentaire:

Enregistrer un commentaire