我需要预留空间来处理某些std::aligned_storage<>
中某种尺寸的类型。以下是确定存储空间最大大小的安全且正确的方法吗?
template <std::size_t size> // need to handle types with a maximum size of 'size'
class storage
{
class holder_base
{
virtual ~holder_base() = default;
};
// will only ever hold 'data' as a data member
// static_assert will ensure sizeof(T) <= size
template <typename T>
class holder : public holder_base
{
T data;
};
// used to determine maximum size, including any added size from inheritance
class holder_size_t : public storage_base
{
char filler[ size ];
};
std::aligned_storage_t<sizeof( holder_size_t )> memory;
};
memory
是否可以保留任何holder<T>
,并且保证可以通过reinterpret_cast<holder_base*>(&memory)
之类的演员无法正确访问,但没有未定义的行为?