It seems to me that there should be four options boost::optional
optional<Foo> => contains mutable Foo and can be reassigned after initialization
optional<Foo const> const => contains the constant Foo and cannot be reassigned after initialization
optional<Foo> const => (should it?) contain mutable Foo, but cannot be reassigned after initialization
optional<Foo const> => (should?) hold const Foo and can be reassigned after initialization
The first 2 cases work as expected. But dereferencing is optional<Foo> constfor const Foo and optional<Foo const>does not allow reassigning after initialization (as discussed in this question ).
Remapping const value types is what I came across and the error:
/usr/include/boost/optional/optional.hpp:486: error: passing 'const Foo as' to this argument' Foo & Foo :: operator = (const Foo &) discards qualifiers [-fpermissive]
And this happens here:
void assign_value(argument_type val,is_not_reference_tag) { get_impl() = val; }
After construction, the implementation uses an assignment operator for the type with which you parameterized an optional parameter. Obviously, he does not want the left operand to be a const value. But why don't you let reset unconstant the optional parameter for the new const value, for example, in this case:
optional<Foo const> theFoo (maybeGetFoo());
while (someCondition) {
theFoo = maybeGetFoo();
}
Some questions:
Do I want it to be conceptually perfect, and the inability to do this is just an accident in implementation?
If I do not edit the sources of boost, then what is the clean way to implement the logic, as in the loop above, without utilizing boost :: optional at all?
, boost:: optional source ( , , , ), - ?