[bug] Error in optional's converting assignment operator

There is an error in the implementation of the converting assignment operator for optional<T>. It is currently implemented as template<class U> optional& operator= ( optional<U> const& rhs ) { this->assign(rhs.get()); return *this ; } The problem with this is that if rhs is not initialized then rhs.get() asserts. This function should be rewritten as template<class U> optional& operator= ( optional<U> const& rhs ) { if (rhs.is_initialized()) this->assign(rhs.get()); else destroy(); return *this ; } Joe Gottman

Joe Gottman wrote:
There is an error in the implementation of the converting assignment operator for optional<T>. It is currently implemented as
template<class U> optional& operator= ( optional<U> const& rhs ) { this->assign(rhs.get()); return *this ; }
The problem with this is that if rhs is not initialized then rhs.get() asserts. This function should be rewritten as
template<class U> optional& operator= ( optional<U> const& rhs ) { if (rhs.is_initialized()) this->assign(rhs.get()); else destroy(); return *this ; }
Yikes! Of course... The fix is not as simple as that (because destroy() invokes UB if '*this' is absent), but, ya, that's the idea. Thank for reporting it! Fernando Cacciola SciSoft

"Joe Gottman" <jgottman@carolina.rr.com> wrote in message news:dp1uie$q48$1@sea.gmane.org...
There is an error in the implementation of the converting assignment operator for optional<T>. It is currently implemented as
template<class U> optional& operator= ( optional<U> const& rhs ) { this->assign(rhs.get()); return *this ; }
The problem with this is that if rhs is not initialized then rhs.get() asserts.
I posted this bug report about a month ago. With the new version of boost coming out, I think that it should be fixed in time for boost 1.34. It seems to me that this is a fairly simple fix: template <class U> optional& operator=(optional<U> const &rhs) { if (rhs.is_initialized()) { this->assign(*rhs); } else if (this->is_initialized()) { this->destroy(); } return *this; } Joe Gottman

Joe Gottman wrote:
"Joe Gottman" <jgottman@carolina.rr.com> wrote in message news:dp1uie$q48$1@sea.gmane.org...
There is an error in the implementation of the converting assignment operator for optional<T>. It is currently implemented as
[SNIPPED]
I posted this bug report about a month ago. With the new version of boost coming out, I think that it should be fixed in time for boost 1.34. You're right... but I was on Vacation pretty much all of Jannuary. I just got back to work this week. I'll fix it today.
Best Fernando Cacciola

On Thu, 2 Feb 2006 11:14:42 -0300 "Fernando Cacciola" <fernando_cacciola@hotmail.com> wrote:
You're right... but I was on Vacation pretty much all of Jannuary. I just got back to work this week. I'll fix it today.
Wow! I took 2 weeks for my 10th wedding anniversary about 7 years ago (went to Paris... can think of a LOT better things to do with 2 weeks of time ;-). Other than that, I don't think I've had more than one week off at any given time... I hope you enjoyed yourself!!!
participants (3)
-
Fernando Cacciola
-
Jody Hagins
-
Joe Gottman