forked from boostorg/utility
Compare commits
4 Commits
svn-branch
...
svn-branch
Author | SHA1 | Date | |
---|---|---|---|
6af8e5d5f9 | |||
561a94c8c6 | |||
9ddcf04abc | |||
b08a86f069 |
@ -54,8 +54,8 @@ public:
|
||||
|
||||
void* apply (void* address, std::size_t n) const
|
||||
{
|
||||
for(char* next = address = this->apply(address); !! --n;)
|
||||
this->apply(next = next+sizeof(T));
|
||||
for(void* next = address = this->apply(address); !! --n;)
|
||||
this->apply(next = static_cast<char *>(next) + sizeof(T));
|
||||
return address;
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,11 @@
|
||||
// (C) 2002, Fernando Luis Cacciola Carballal.
|
||||
// (C) Copyright 2002-2007, Fernando Luis Cacciola Carballal.
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
// 21 Ago 2002 (Created) Fernando Cacciola
|
||||
// 07 Set 2007 (Worked around MSVC++ bug) Fernando Cacciola, Niels Dekker
|
||||
//
|
||||
#ifndef BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
||||
#define BOOST_UTILITY_VALUE_INIT_21AGO2002_HPP
|
||||
@ -13,6 +14,98 @@
|
||||
#include <boost/type_traits/cv_traits.hpp>
|
||||
#include <boost/detail/workaround.hpp>
|
||||
|
||||
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500) )
|
||||
// Microsoft Visual C++ does not correctly support value initialization, as reported by
|
||||
// Pavel Kuznetsov (MetaCommunications Engineering), 7/28/2005, Feedback ID 100744,
|
||||
// Feedback Title: Value-initialization in new-expression
|
||||
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744
|
||||
// The report was closed at 11/14/2006, and its status was set to "Closed (Won't Fix)".
|
||||
// Luckily, even in the presence of this compiler bug, boost::value_initialized will still
|
||||
// do its job correctly, when using the following workaround:
|
||||
#define BOOST_UTILITY_VALUE_INIT_WORKAROUND
|
||||
#endif
|
||||
|
||||
#ifdef BOOST_UTILITY_VALUE_INIT_WORKAROUND
|
||||
#include <boost/aligned_storage.hpp>
|
||||
#include <boost/type_traits/alignment_of.hpp>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
#pragma warning(push)
|
||||
#if _MSC_VER >= 1310
|
||||
// When using MSVC 7.1 or higher, placement new, "new (&x) T()", may trigger warning C4345:
|
||||
// "behavior change: an object of POD type constructed with an initializer of the form ()
|
||||
// will be default-initialized". There is no need to worry about this, though.
|
||||
#pragma warning(disable: 4345)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace vinit_detail {
|
||||
|
||||
template<class T>
|
||||
class const_T_base
|
||||
{
|
||||
protected :
|
||||
|
||||
const_T_base()
|
||||
{
|
||||
std::memset(&x, 0, sizeof(x));
|
||||
new (&x) T();
|
||||
}
|
||||
|
||||
~const_T_base()
|
||||
{
|
||||
void const * ptr = &x;
|
||||
static_cast<T*>(ptr)->T::~T();
|
||||
}
|
||||
|
||||
T & get() const
|
||||
{
|
||||
void const * ptr = &x;
|
||||
return *static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
private :
|
||||
typename ::boost::aligned_storage<sizeof(T), ::boost::alignment_of<T>::value>::type x;
|
||||
} ;
|
||||
|
||||
template<class T>
|
||||
class non_const_T_base
|
||||
{
|
||||
protected :
|
||||
|
||||
non_const_T_base()
|
||||
{
|
||||
std::memset(&x, 0, sizeof(x));
|
||||
new (&x) T();
|
||||
}
|
||||
|
||||
~non_const_T_base()
|
||||
{
|
||||
void * ptr = &x;
|
||||
static_cast<T*>(ptr)->T::~T();
|
||||
}
|
||||
|
||||
T & get() const
|
||||
{
|
||||
void * ptr = &x;
|
||||
return *static_cast<T*>(ptr);
|
||||
}
|
||||
|
||||
private :
|
||||
mutable typename ::boost::aligned_storage<sizeof(T), ::boost::alignment_of<T>::value>::type x;
|
||||
} ;
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
// Restores the state of warning C4345.
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
namespace boost {
|
||||
|
||||
namespace vinit_detail {
|
||||
@ -23,20 +116,26 @@ class const_T_base
|
||||
protected :
|
||||
|
||||
const_T_base() : x() {}
|
||||
T & get() const { return x; }
|
||||
|
||||
private :
|
||||
T x ;
|
||||
} ;
|
||||
|
||||
template<class T>
|
||||
struct non_const_T_base
|
||||
class non_const_T_base
|
||||
{
|
||||
protected :
|
||||
|
||||
non_const_T_base() : x() {}
|
||||
T & get() const { return x; }
|
||||
|
||||
private :
|
||||
mutable T x ;
|
||||
} ;
|
||||
|
||||
#endif
|
||||
|
||||
template<class T>
|
||||
struct select_base
|
||||
{
|
||||
@ -57,9 +156,9 @@ class value_initialized : private vinit_detail::select_base<T>::type
|
||||
|
||||
value_initialized() {}
|
||||
|
||||
operator T&() const { return this->x ; }
|
||||
operator T&() const { return this->get(); }
|
||||
|
||||
T& data() const { return this->x ; }
|
||||
T& data() const { return this->get(); }
|
||||
|
||||
} ;
|
||||
|
||||
|
@ -60,6 +60,20 @@ struct NonPOD : NonPODBase
|
||||
std::string id ;
|
||||
} ;
|
||||
|
||||
//
|
||||
// Sample aggregate POD struct type
|
||||
//
|
||||
struct AggregatePODStruct
|
||||
{
|
||||
float f;
|
||||
char c;
|
||||
int i;
|
||||
};
|
||||
|
||||
bool operator == ( AggregatePODStruct const& lhs, AggregatePODStruct const& rhs )
|
||||
{ return lhs.f == rhs.f && lhs.c == rhs.c && lhs.i == rhs.i ; }
|
||||
|
||||
|
||||
template<class T>
|
||||
void test ( T const& y, T const& z )
|
||||
{
|
||||
@ -95,6 +109,13 @@ int test_main(int, char **)
|
||||
test( POD(0,0,0.0), POD('a',1234,56.78) ) ;
|
||||
test( NonPOD( std::string() ), NonPOD( std::string("something") ) ) ;
|
||||
|
||||
NonPOD NonPOD_object( std::string("NonPOD_object") );
|
||||
test<NonPOD *>( 0, &NonPOD_object ) ;
|
||||
|
||||
AggregatePODStruct zeroInitializedAggregatePODStruct = { 0.0f, '\0', 0 };
|
||||
AggregatePODStruct nonZeroInitializedAggregatePODStruct = { 1.25f, 'a', -1 };
|
||||
test(zeroInitializedAggregatePODStruct, nonZeroInitializedAggregatePODStruct);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user