为了测试特定类型是否适合aligned_storage,我创建了以下测试结构:
template< typename T, std::size_t Bytes >
struct fits_in_storage : public std::integral_constant<bool, sizeof(std::aligned_storage<Bytes>::type) >= sizeof(std::aligned_storage<sizeof(T)>::type)>
{};现在我有点想知道这样的测试是否会出现在stdlib中。都不愿意重新发明轮子。
我使用它来检查头部定义的aligned_storage (大小为Bytes)是否可以采用内部数据类型,这只在实际的编译单元中可用。
发布于 2013-09-30 22:15:45
除非至少有Len字节,否则不能保证aligned_storage<Len, Align>::type的大小。有可能(但不太可能)较小的Len的::type比较大的Len更大。
meta.trans.other状态,用于
aligned_storage<std::size_t Len, std::size_t Align =default-alignment>
对于大小不大于Len (3.9)的任何C++对象类型,默认对齐值应是最严格的对齐要求。成员类型类型应该是POD类型,适合用作任何对象的未初始化存储,该对象的大小最大为Len,对齐方式为Align的除数。
因此,任何小于或等于Len的对象都可以存储在aligned_storage<Len>::type中。因此,您的检查可以简化为:
template< typename T, std::size_t Bytes >
struct fits_in_storage
: public std::integral_constant<bool, (Bytes >= sizeof(T))>
{};当然,它可以简化为Bytes >= sizeof(T)。
https://stackoverflow.com/questions/19095272
复制相似问题