[Optional] Lazy initialized optional
Hello, I am looking into deriving from boost::optional and adding lazy initialization capability. I can arrange a constructor, or whatever, to inject an appropriate factory method, functional callback, etc, that's no problem.
From the boost::optional perspective, I'd like to override a key method (or methods, hopefully not too many) in order to inject that factory provided value. I'm thinking optional::get?
Unless there's a better override for that? Cheers, Michael Powell
I think using an optional for this is not the right way. Optional has a storage overhead, because it needs to store whether or not the optional has a value. If you derive this class you are going to have this overhead, plus of course the storage for your callback. If you do it in a separate class instead, you can clear the callback after invoking it. Then checking whether a callback exists will tell you whether the object was initialized. On Tue, 2018-11-13 at 12:14 -0500, Michael Powell via Boost-users wrote:
Hello,
I am looking into deriving from boost::optional and adding lazy initialization capability.
I can arrange a constructor, or whatever, to inject an appropriate factory method, functional callback, etc, that's no problem.
From the boost::optional perspective, I'd like to override a key method (or methods, hopefully not too many) in order to inject that factory provided value. I'm thinking optional::get?
Unless there's a better override for that?
Cheers,
Michael Powell _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users
On Wed, Nov 14, 2018 at 5:27 PM Gavin Lambert via Boost-users
On 14/11/2018 23:03, Martijn Otto wrote:
If you do it in a separate class instead, you can clear the callback after invoking it. Then checking whether a callback exists will tell you whether the object was initialized.
Unless the callback itself can produce an optional value.
It was simple enough. Instead of an "is an optional" use case it was more of a "has an optional" use case, with the optional being a one-time, factory initialized value. Worked pretty well. :+1: I even managed to leverage the optional operator->() overload in order to access the thing inside the optional post-initialization.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org https://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Gavin Lambert
-
Martijn Otto
-
Michael Powell