value_init.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // (C) Copyright 2002-2008, Fernando Luis Cacciola Carballal.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // 21 Ago 2002 (Created) Fernando Cacciola
  8. // 24 Dec 2007 (Refactored and worked around various compiler bugs) Fernando Cacciola, Niels Dekker
  9. // 23 May 2008 (Fixed operator= const issue, added initialized_value) Niels Dekker, Fernando Cacciola
  10. // 21 Ago 2008 (Added swap) Niels Dekker, Fernando Cacciola
  11. //
  12. #ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
  13. #define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
  14. // Note: The implementation of boost::value_initialized had to deal with the
  15. // fact that various compilers haven't fully implemented value-initialization.
  16. // The constructor of boost::value_initialized<T> works around these compiler
  17. // issues, by clearing the bytes of T, before constructing the T object it
  18. // contains. More details on these issues are at libs/utility/value_init.htm
  19. #include <boost/aligned_storage.hpp>
  20. #include <boost/detail/workaround.hpp>
  21. #include <boost/static_assert.hpp>
  22. #include <boost/type_traits/cv_traits.hpp>
  23. #include <boost/type_traits/alignment_of.hpp>
  24. #include <boost/swap.hpp>
  25. #include <cstring>
  26. #include <new>
  27. namespace boost {
  28. template<class T>
  29. class value_initialized
  30. {
  31. private :
  32. struct wrapper
  33. {
  34. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
  35. typename
  36. #endif
  37. remove_const<T>::type data;
  38. };
  39. mutable
  40. #if !BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x592))
  41. typename
  42. #endif
  43. aligned_storage<sizeof(wrapper), alignment_of<wrapper>::value>::type x;
  44. wrapper * wrapper_address() const
  45. {
  46. return static_cast<wrapper *>( static_cast<void*>(&x));
  47. }
  48. public :
  49. value_initialized()
  50. {
  51. std::memset(&x, 0, sizeof(x));
  52. #ifdef BOOST_MSVC
  53. #pragma warning(push)
  54. #if _MSC_VER >= 1310
  55. // When using MSVC 7.1 or higher, the following placement new expression may trigger warning C4345:
  56. // "behavior change: an object of POD type constructed with an initializer of the form ()
  57. // will be default-initialized". It is safe to ignore this warning when using value_initialized.
  58. #pragma warning(disable: 4345)
  59. #endif
  60. #endif
  61. new (wrapper_address()) wrapper();
  62. #ifdef BOOST_MSVC
  63. #pragma warning(pop)
  64. #endif
  65. }
  66. value_initialized(value_initialized const & arg)
  67. {
  68. new (wrapper_address()) wrapper( static_cast<wrapper const &>(*(arg.wrapper_address())));
  69. }
  70. value_initialized & operator=(value_initialized const & arg)
  71. {
  72. // Assignment is only allowed when T is non-const.
  73. BOOST_STATIC_ASSERT( ! is_const<T>::value );
  74. *wrapper_address() = static_cast<wrapper const &>(*(arg.wrapper_address()));
  75. return *this;
  76. }
  77. ~value_initialized()
  78. {
  79. wrapper_address()->wrapper::~wrapper();
  80. }
  81. T& data() const
  82. {
  83. return wrapper_address()->data;
  84. }
  85. void swap(value_initialized & arg)
  86. {
  87. ::boost::swap( this->data(), arg.data() );
  88. }
  89. operator T&() const { return this->data(); }
  90. } ;
  91. template<class T>
  92. T const& get ( value_initialized<T> const& x )
  93. {
  94. return x.data() ;
  95. }
  96. template<class T>
  97. T& get ( value_initialized<T>& x )
  98. {
  99. return x.data() ;
  100. }
  101. template<class T>
  102. void swap ( value_initialized<T> & lhs, value_initialized<T> & rhs )
  103. {
  104. lhs.swap(rhs) ;
  105. }
  106. class initialized_value_t
  107. {
  108. public :
  109. template <class T> operator T() const
  110. {
  111. return get( value_initialized<T>() );
  112. }
  113. };
  114. initialized_value_t const initialized_value = {} ;
  115. } // namespace boost
  116. #endif