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) {}
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 ?
Edit (clarification)
I would like to have boost::optional<A> op_
class data member but I don't know how to initialize it.
You can declare the constructor of B
as
class B {
public:
B(std::string p1, int p2) :
op_(boost::in_place<A>(std::move(p1), p2)) {}
private:
const boost::optional<A> op_;
};
and instantiate B
as
B b("abc", 5);
Note that I change the data member op_
to not be reference here, as opposed to the definition of B
in your question.