
On Wed, Nov 14, 2012 at 3:39 PM, Adam Badura <abadura@o2.pl> wrote:
I use boost::optional for deferred construction.
Something similar to the idea described in "Bypassing expensive unnecessary default construction" in Boost.Optional documentation. Only in my case I do this because the mentioned construction requires virtual function calls and this cannot be done in owning object constructor and has to be postponed.
So instead of accessing the object directly I always access it by a function. That function first checks if the object is already constructed (boost::optional is initialized) and if so just returns it. Otherwise it first constructs it.
That approach requires only copy construction (or even move construction if boost::optional would support moving in the first place). Yet due to implementation details the embedded type has to be assignable as well. Even thou assignment operator will never be called (in this scenario).
Is there a way to avoid this issue?
Now the Synopsis part of the Boost.Optional documentation does mention (Typed)InPlaceFactory assign operators which do look promising for solving this issue. But firstly they are not documented in following Detailed Semantics part. There is even no link in the Synopsis. (Is this docs bug? In which part? Missing docs or extra functions?) And secondly (Typed)InPlaceFactory support is "intrusive". I cannot add it to a class without changing the class itself. And in my case I cannot change the class (boost::program_options::options_description - why cannot it be copy assigned, that I don't know...). Maybe adding some extra wrapper would help but I don't think it would be a solution. Rather a somewhat ugly workaround.
In-place factories are not intrusive, you can in-place construct value of any type, as long as it has the necessary constructor. The description is available here: http://www.boost.org/doc/libs/release/libs/optional/doc/html/boost_optional/... AFAIR, assignment operator is not required by optional if you use in-place factories in assignment. It is only required if you use regular assignment with optional.