Boost logo

Boost Users :

Subject: [Boost-users] ~optional<int> generates unnessesary code
From: Hite, Christopher (Christopher.Hite_at_[hidden])
Date: 2012-01-23 06:41:46


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 <boost/optional.hpp>

typedef boost::optional<int> optional_int;

void deconstruct_boost_optional(optional_int& o){
        o.~optional_int();
}

------------------

objdump -f -d -C -l boost_optional_deconstruction.o |c++filt

00000000 <deconstruct_boost_optional(boost::optional<int>&)>:
deconstruct_boost_optional(boost::optional<int>&)():
/home/eh2hite/chris/src_root/src/test/boost_optional_deconstruction.cpp:5
   0: 8b 44 24 04 mov 0x4(%esp),%eax
boost::optional_detail::optional_base<int>::destroy()():
/usr/local/include/boost/optional/optional.hpp:407
   4: 80 38 00 cmpb $0x0,(%eax)
   7: 74 03 je c <deconstruct_boost_optional(boost::optional<int>&)+0xc>
boost::optional_detail::optional_base<int>::destroy_impl(mpl_::bool_<false>)():
/usr/local/include/boost/optional/optional.hpp:434
   9: c6 00 00 movb $0x0,(%eax)
   c: f3 c3 repz ret



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net