
2012/11/14 Adam Badura <abadura@o2.pl>
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.
I am having problems imagining the situation. Can't you change your function so that returns real object rather than optional object if it is initialized? Perhaps using optional reference to an object would solve your problem?
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?)
See this link: http://www.boost.org/doc/libs/1_52_0/libs/utility/in_place_factories.html Indeed, there is no path to it from the documentation root.
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.
Intrusive? "in_place" is used to initialize or assign boost::optional. Do you want to use it for your own class? THis does not appear the right way to go. Regards, &rzej