
I have a situation where 'optional' is a good fit. However, I can't quite figure out how to use it for what I want. It appears to be geared around passing and returning instances, but I want to use it as a member. Consider: class C { D* d; // ... real code has much more stuff, naturally public: D(params) : d(0) { // ... if (I_ended_up_needing_d) d= new D (blahblah); // ... } ~D() { delete d; // harmless if zero } }; There is a single ownership of d by the instance, and it is not polymorphic but an exact type known at compile time. Making it a pointer adds overhead of using the heap, makes it harder to write EH-safe code for the constructor in more realistic situations, and requires adding explicit code to the destructor. Using a smart pointer only adds overhead, might take care of the EH and housekeeping, but still uses the heap. The 'optional' class provides for in-place storage, which it can use for an initialized instance, or it could be nil. That's exactly what I want. But, how do I tell optional<D> to construct at some point within the 'if' statement of my code? Assigning requires duplicating the object, and this type is not meant to be copied and has copy constructing and assignment disabled. Based on how the class works, I should be able to say "turn it on, now" and use an in-place factory. But the function to do that is not present in the class. class C { optional<D> d; public: D(params) : d(boost::none_t) { if (I_ended_up_needing_d) d.reset (in_place(blahblah)); } }; Am I missing something, or is the class missing something? I'm using Boost 1.43 in the project, and basing the above on the documentation for that version. The 'news' for 1.44 does not list Optional as having changed. Thanks, --John (sorry about the corporate footer) TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

On 10/24/2010 7:41 PM, John Dlugosz wrote:
I have a situation where 'optional' is a good fit. However, I can't quite figure out how to use it for what I want. It appears to be geared around passing and returning instances, but I want to use it as a member. Consider:
class C { D* d; // ... real code has much more stuff, naturally public: D(params) : d(0) { // ... if (I_ended_up_needing_d) d= new D (blahblah); // ... } ~D() { delete d; // harmless if zero } };
There is a single ownership of d by the instance, and it is not polymorphic but an exact type known at compile time. Making it a pointer adds overhead of using the heap, makes it harder to write EH-safe code for the constructor in more realistic situations, and requires adding explicit code to the destructor. Using a smart pointer only adds overhead, might take care of the EH and housekeeping, but still uses the heap.
The 'optional' class provides for in-place storage, which it can use for an initialized instance, or it could be nil. That's exactly what I want. But, how do I tell optional<D> to construct at some point within the 'if' statement of my code? Assigning requires duplicating the object, and this type is not meant to be copied and has copy constructing and assignment disabled.
Based on how the class works, I should be able to say "turn it on, now" and use an in-place factory. But the function to do that is not present in the class.
class C { optional<D> d; public: D(params) : d(boost::none_t) { if (I_ended_up_needing_d) d.reset (in_place(blahblah)); } };
Am I missing something, or is the class missing something?
Just assign your D instance to 'd' when you want to do so.

On Mon, Oct 25, 2010 at 9:48 AM, Edward Diener
On 10/24/2010 7:41 PM, John Dlugosz wrote:
Am I missing something, or is the class missing something?
Just assign your D instance to 'd' when you want to do so.
I think what the OP was looking for is a move constructor for Boost.Optional which I don't think it supports (yet). -- Dean Michael Berris deanberris.com

On Mon, Oct 25, 2010 at 6:11 PM, Alexander Churanov
2010/10/25 Dean Michael Berris
: I think what the OP was looking for is a move constructor for Boost.Optional which I don't think it supports (yet).
Isn't std::auto_ptr a suitable alternative for this case?
I'd think so, but the OP wants to avoid the heap allocation. -- Dean Michael Berris deanberris.com

Just assign your D instance to 'd' when you want to do so.
I think what the OP was looking for is a move constructor for Boost.Optional which I don't think it supports (yet).
--
I don't want to create a different D instance and then assign it to d, since D cannot be copied. If I were using a compiler that supported move references, I could add a move assignment to class D, but I'm not. I want to say "construct d with these constructor arguments, now." new optional<D> ((void*)&d) (in_place(args)); might do the trick in the special case where it was indeed uninitialized before (it doesn't call the destructor of the old value), but that's a dirty trick. I show it to better indicate what I'm after: initialize (not assign) from arguments, within the body of an if statement, not at the normal initializer. I think d.reset(in_place(args)) would do the trick, so I propose that as a logical missing consequence of existing features. --John ---corporate footer--- TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

On 10/28/2010 11:27 AM, John Dlugosz wrote:
I think d.reset(in_place(args)) would do the trick, so I propose that as a logical missing consequence of existing features.
I was (independently) looking for this today and came across this recent thread. So I agree with you that it seems like natural functionality that is conspicuously missing. Optional even has a construct(in_place) method, but it's protected. I need to declare a variable but I'm not ready to construct it yet. The type represents an operating system resource so it doesn't have meaningful copy semantics and designing it differently is not possible. Std::auto_ptr would work except there's no good reason for this to require a heap allocation. Boost::optional is exactly what I want, it's just that it unnecessarily requires a copy for deferred initialization. - Marsh

On Sun, Nov 28, 2010 at 8:47 AM, Marsh Ray
On 10/28/2010 11:27 AM, John Dlugosz wrote:
I think d.reset(in_place(args)) would do the trick, so I propose that as a logical missing consequence of existing features.
I was (independently) looking for this today and came across this recent thread. So I agree with you that it seems like natural functionality that is conspicuously missing.
I'm not sure you understood the resolution of the discussion -- you will need to use the Boost.In_place_factory utility if you want to construct the resource in-place.
Optional even has a construct(in_place) method, but it's protected.
Just assign an in-place factory. Look at http://www.boost.org/doc/libs/1_45_0/libs/utility/in_place_factories.html for more details.
I need to declare a variable but I'm not ready to construct it yet. The type represents an operating system resource so it doesn't have meaningful copy semantics and designing it differently is not possible.
Std::auto_ptr would work except there's no good reason for this to require a heap allocation.
Boost::optional is exactly what I want, it's just that it unnecessarily requires a copy for deferred initialization.
It's not required. You can use the in place factory for that. optional<foo> o; o = in_place<foo>(parameters, to, foo); HTH -- Dean Michael Berris deanberris.com

On 11/27/2010 06:57 PM, Dean Michael Berris wrote:
I'm not sure you understood the resolution of the discussion -- you will need to use the Boost.In_place_factory utility if you want to construct the resource in-place.
Haha, probably not. I admit to skimming it quickly mostly to confirm my preconceptions. Looks like there were a couple more emails in the thread.
On Sun, Nov 28, 2010 at 8:47 AM, Marsh Ray
wrote: Boost::optional is exactly what I want, it's just that it unnecessarily requires a copy for deferred initialization.
It's not required. You can use the in place factory for that.
optional<foo> o; o = in_place<foo>(parameters, to, foo);
Cool, it works! (Rather it compiles, so I'll assume it works for now.) Still, the documentation says it shouldn't: http://www.boost.org/doc/libs/1_45_0/libs/optional/doc/html/boost_optional/d...
optional& optional
::operator= ( T const& rhs ) ;
Notes: If *this was initialized, T's assignment operator is used, otherwise, its copy-constructor is used.
No mention of using in_place with operator=. Thanks, - Marsh

Other answers to my original post prompted me to look closer at the header file. Besides reset() being deprecated in favor of operator=, the implementation of the latter includes a generic forwarding mechanism that naturally works for the same things the constructor does. So it's either an unintended accident, or poor documentation, but assigning a in-place should work.
-----Original Message----- From: Marsh Ray [mailto:marsh@extendedsubset.com] Sent: Saturday, November 27, 2010 6:47 PM To: boost-users@lists.boost.org Cc: John Dlugosz Subject: Re: Usage of Boost.Optional
On 10/28/2010 11:27 AM, John Dlugosz wrote:
I think d.reset(in_place(args)) would do the trick, so I propose that as a logical missing consequence of existing features.
I was (independently) looking for this today and came across this recent thread. So I agree with you that it seems like natural functionality that is conspicuously missing.
Optional even has a construct(in_place) method, but it's protected.
TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

Hi!
2010/10/25, John Dlugosz
Am I missing something, or is the class missing something?
just write: d = in_place(blahblah...); instead. And note that "reset" method is deprecated. See http://www.boost.org/doc/libs/1_44_0/libs/optional/doc/html/boost_optional/s... for the class interface.

2010/10/25, John Dlugosz
: Am I missing something, or is the class missing something?
just write:
d = in_place(blahblah...);
instead. And note that "reset" method is deprecated. See http://www.boost.org/doc/libs/1_44_0/libs/optional/doc/html/boost_optio nal/synopsis.html for the class interface.
Thanks. I checked the 1_43 code, and it appears to allow that. So it's just the docs that were lacking. Also note that http://www.boost.org/users/news/version_1_44_0 does not indicate that Optional has been updated. TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.
participants (6)
-
Alexander Churanov
-
Dean Michael Berris
-
Edward Diener
-
John Dlugosz
-
Marsh Ray
-
TONGARI