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:

                // 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.



#include <boost/optional.hpp>

typedef boost::optional<int> optional_int;

void deconstruct_boost_optional(optional_int& o){


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

00000000 <deconstruct_boost_optional(boost::optional<int>&)>:
   0: 8b 44 24 04 mov 0x4(%esp),%eax
   4: 80 38 00 cmpb $0x0,(%eax)
   7: 74 03 je c <deconstruct_boost_optional(boost::optional<int>&)+0xc>
   9: c6 00 00 movb $0x0,(%eax)
   c: f3 c3 repz ret

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at