diff --git a/include/boost/optional/detail/optional_aligned_storage.hpp b/include/boost/optional/detail/optional_aligned_storage.hpp index 6c7d5814..df41e770 100644 --- a/include/boost/optional/detail/optional_aligned_storage.hpp +++ b/include/boost/optional/detail/optional_aligned_storage.hpp @@ -32,6 +32,9 @@ class aligned_storage #endif dummy_u { + // empty_initiliazer_ is used only to shut down gcc warnings about + // data "may be used unitialized" + char empty_initiliazer_; char data[ sizeof(T) ]; BOOST_DEDUCED_TYPENAME type_with_alignment< ::boost::alignment_of::value >::type aligner_; @@ -47,6 +50,11 @@ class aligned_storage void * address() { return dummy_.data; } #endif + void construct_empty() + { + dummy_.empty_initiliazer_ = '\0'; + } + #if defined(BOOST_OPTIONAL_DETAIL_USE_ATTRIBUTE_MAY_ALIAS) // This workaround is supposed to silence GCC warnings about broken strict aliasing rules T const* ptr_ref() const diff --git a/include/boost/optional/optional.hpp b/include/boost/optional/optional.hpp index ff1a16ca..04479e22 100644 --- a/include/boost/optional/optional.hpp +++ b/include/boost/optional/optional.hpp @@ -89,13 +89,19 @@ class optional_base : public optional_tag // No-throw optional_base() : - m_initialized(false) {} + m_initialized(false) + { + construct_empty(); + } // Creates an optional uninitialized. // No-throw optional_base ( none_t ) : - m_initialized(false) {} + m_initialized(false) + { + construct_empty(); + } // Creates an optional initialized with 'val'. // Can throw if T::T(T const&) does @@ -125,6 +131,8 @@ class optional_base : public optional_tag { if ( cond ) construct(val); + else + construct_empty(); } // Creates a deep copy of another optional @@ -135,6 +143,8 @@ class optional_base : public optional_tag { if ( rhs.is_initialized() ) construct(rhs.get_impl()); + else + construct_empty(); } #ifndef BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES @@ -146,6 +156,8 @@ class optional_base : public optional_tag { if ( rhs.is_initialized() ) construct( boost::move(rhs.get_impl()) ); + else + construct_empty(); } #endif @@ -320,6 +332,11 @@ class optional_base : public optional_tag protected : + void construct_empty () + { + m_storage.construct_empty(); + } + void construct ( argument_type val ) { ::new (m_storage.address()) value_type(val) ;