4 ms·
https://www.boost.org/doc/libs/1_34_1/boost/optional/optional.hpp https://www.boost.org/doc/libs/1_34_1/boost/optional/optiona... bool m_initialized ;
by morelisp 3y ago
https://www.boost.org/doc/libs/1_34_1/boost/optional/optional.hpp https://www.boost.org/doc/libs/1_34_1/boost/optional/optiona...
bool m_initialized ;
storage_type m_storage ;
- zabzonk 3y agook, now explain how this " conflate memory layout and type definitions"
- Dylan16807 3y agoIt sticks a bool onto each value, directly attached in memory layout, taking up a byte plus padding bytes, multiplied by how many values you have. (The compiler is allowed to improve this, but it won't.)
- morelisp 3y agoThe compiler is only vaguely allowed to do this by general "as if" rules; in practice there are so many formal constraints on field layout I don't think it could do better even with the world's most brilliant optimizer. It would need to behave "as if" if the two fields are laid out next to each other, in address-ascending order, with nothing but padding in between.
- gpderetta 3y agoBoost and std optional do not do it, but you can write your optional type that take advantage of the properties of T for optimal storage via traits. FWIW, we have had this discussion a few months ago on HN.