lundi 1 avril 2019

How to initialize non-movable and non-copyable class member - boost::optional

I have a non-movable and non-copyable type:

struct A
{
    A(std::string p1, int p2){}
    A(A const &) = delete;
    A(A&&) = delete;
    A& operator=(A const &) = delete;
    A& operator=(A&) = delete;
};

I can construct boost optional this way:

boost::optional<A> op(boost::in_place("abc", 5));

I also need to initialize boost::optional<A> which is a class member. Here is my solution:

class B
{
public:
    B(const boost::optional<A>& op): op_(op) {} //this of course is an error 
private:
    const boost::optional<A>& op_;
};

B b(boost::optional<A>(boost::in_place("abc", 5)));

Is it possible to have just boost::optional<A> class member and initialize it somehow ?

Aucun commentaire:

Enregistrer un commentaire