~optional<int> generates unnessesary code
The problem is that m_initialized is checked and set during deconstruction. This generates a bunch of useless code for types with trivial deconstruction:
if(m_initialized){
// do nothing
m_initialized = false;
}
The code is more than useless since it contains a branch. In my case I've got a big object I would expect to die without peep, branching and writing to memory for no reason.
The steps below show what gcc 4.6.0 generates. You'd expect the function to do nothing.
Boost optional has no obligation to maintain state during deconstruction (otherwise you'd need states for deconstructing,constructing). It's not responsible for the bytes left behind after deconstruction.
By the way boost::variant does do this correctly.
Chris
------------------
#include
Nobody seems to want to bite here. I found more issues with optional which make it expensive.
1) When assigning it checks m_initialized to see if it needs to use the copy constructor or operator= . That also makes no sense for POD types.
2) has_trivial_copy< optional<int> >::value should be true, just like for std::pair
Hite, Christopher wrote:
Nobody seems to want to bite here. I found more issues with optional which make it expensive. ... I'd be willing to put it some time. Who do I talk to? Am I posting to the wrong list?
The developer list might be more appropriate (specify title starting with [optional], and/or create a trac ticket at: https://svn.boost.org/trac/boost/newticket. See the page: http://www.boost.org/support/bugs.html for more info. Your ideas seem useful. Jeff
participants (2)
-
Hite, Christopher
-
Jeff Flinn