如何复制 boost::aligned_storage 对象中的数据?
How data in a boost::aligned_storage object is copied?
boost::aligned_storage
数据类型对我很有用,可以在 c++11 之前的世界中提供对齐存储。我有一个包含此存储成员的 class:
template <size_t StoreSize>
class RoutineStorage {
enum { ROUTINE_STORAGE_SIZE = StoreSize};
enum { BUFFER_ALIGNMENT_VALUE = 8 };
template <typename TStorageType> TStorageType& getStorageAsType()
{
BOOST_STATIC_ASSERT_MSG(boost::has_trivial_assign<TStorageType>::value &&
boost::has_trivial_copy<TStorageType>::value,
"The storage type must be trvially copyable and assignable to support this classes "
"copy|assign semantics.");
... // Checking and some other code.
return new (_store.address()) TStorageType();
}
private:
typedef boost::aligned_storage<ROUTINE_STORAGE_SIZE, BUFFER_ALIGNMENT_VALUE>
StorageBuffer;
StorageBuffer _store;
}
我想为此 class 提供一个复制构造函数,但是当我查看 aligned_storage
的实现时,它有一个列为私有的复制构造函数和注释 // noncopyable
.在任何关于这种类型的提升页面中似乎都没有对此的解释,所以我得出结论,他们不想处理不同可能的模板化缓冲区大小的复制。我怀疑以下内容可以用于复制此缓冲区:
RoutineStorage(const RoutineStorage<StoreSize>& copy)
{
std::memcpy(_store.address(), copy._store.address(), _store.size())
}
这样会不会有问题?据我所知,aligned_buffer
address
函数将给出连续内存地址的开始,而 size
将让我始终复制正确的大小。
像在
中那样复制缓冲区
RoutineStorage(const RoutineStorage<StoreSize>& copy)
{
std::memcpy(_store.address(), copy._store.address(), _store.size())
}
还不够。是的,您将拥有一个精确的副本,但您实际上并没有在 StorageBuffer
中创建的对象。 [intro.object] 表示
The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition ([basic.def]), by a new-expression, when implicitly changing the active member of a union ([class.union]), or when a temporary object is created ([conv.rval], [class.temporary]).
因此,在您将对象复制到 store
并放置新内容之前,您实际上并没有对象,只有存储空间。
假设您正在存储 Foo
。最初您会在 StorageBuffer
中创建 Foo
,例如
Foo* f = new(_store.address()) Foo();
因此,在复制构造函数中,您只需调用 Foo
的复制构造函数并将其放入 _store
中,如
RoutineStorage(const RoutineStorage<StoreSize>& copy)
{
f = new(_store.address()) Foo(copy.*f);
}
boost::aligned_storage
数据类型对我很有用,可以在 c++11 之前的世界中提供对齐存储。我有一个包含此存储成员的 class:
template <size_t StoreSize>
class RoutineStorage {
enum { ROUTINE_STORAGE_SIZE = StoreSize};
enum { BUFFER_ALIGNMENT_VALUE = 8 };
template <typename TStorageType> TStorageType& getStorageAsType()
{
BOOST_STATIC_ASSERT_MSG(boost::has_trivial_assign<TStorageType>::value &&
boost::has_trivial_copy<TStorageType>::value,
"The storage type must be trvially copyable and assignable to support this classes "
"copy|assign semantics.");
... // Checking and some other code.
return new (_store.address()) TStorageType();
}
private:
typedef boost::aligned_storage<ROUTINE_STORAGE_SIZE, BUFFER_ALIGNMENT_VALUE>
StorageBuffer;
StorageBuffer _store;
}
我想为此 class 提供一个复制构造函数,但是当我查看 aligned_storage
的实现时,它有一个列为私有的复制构造函数和注释 // noncopyable
.在任何关于这种类型的提升页面中似乎都没有对此的解释,所以我得出结论,他们不想处理不同可能的模板化缓冲区大小的复制。我怀疑以下内容可以用于复制此缓冲区:
RoutineStorage(const RoutineStorage<StoreSize>& copy)
{
std::memcpy(_store.address(), copy._store.address(), _store.size())
}
这样会不会有问题?据我所知,aligned_buffer
address
函数将给出连续内存地址的开始,而 size
将让我始终复制正确的大小。
像在
中那样复制缓冲区RoutineStorage(const RoutineStorage<StoreSize>& copy)
{
std::memcpy(_store.address(), copy._store.address(), _store.size())
}
还不够。是的,您将拥有一个精确的副本,但您实际上并没有在 StorageBuffer
中创建的对象。 [intro.object] 表示
The constructs in a C++ program create, destroy, refer to, access, and manipulate objects. An object is created by a definition ([basic.def]), by a new-expression, when implicitly changing the active member of a union ([class.union]), or when a temporary object is created ([conv.rval], [class.temporary]).
因此,在您将对象复制到 store
并放置新内容之前,您实际上并没有对象,只有存储空间。
假设您正在存储 Foo
。最初您会在 StorageBuffer
中创建 Foo
,例如
Foo* f = new(_store.address()) Foo();
因此,在复制构造函数中,您只需调用 Foo
的复制构造函数并将其放入 _store
中,如
RoutineStorage(const RoutineStorage<StoreSize>& copy)
{
f = new(_store.address()) Foo(copy.*f);
}