1. std::aligned_storage
插播一下POD的含义:Plain old data structure,缩写为POD,是C++语言的标准中定义的一类数据结构,POD适用于需要明确的数据底层操作的系统中。POD通常被用在系统的边界处,即指不同系统之间只能以底层数据的形式进行交互,系统的高层逻辑不能互相兼容。比如当对象的字段值是从外部数据中构建时,系统还没有办法对对象进行语义检查和解释,这时就适用POD来存储数据。
可能的源码实现:
// PR c++/56859
// { dg-require-effective-target c++11 }
template
struct aligned_storage
{
using type = struct { alignas(alignment) unsigned char data[size]; };
};
template
struct aligned_storage
{
using type __attribute__((aligned((Align)))) =
char[Len];
};
// PR c++/17743
template
struct aligned_storage
{
typedef char type[Len] __attribute__((aligned((Align))));
};
示例代码:
#include
#include
#include
template
class static_vector
{
// N 个 T 的正确对齐的未初始化存储
typename std::aligned_storage::type data[N];
std::size_t m_size = 0;
public:
// 于对齐存储创建对象
template void emplace_back(Args&&... args)
{
if( m_size >= N ) // 可行的错误处理
throw std::bad_alloc{};
new(data+m_size) T(std::forward(args)...);
++m_size;
}
// 访问对齐存储中的对象
const T& operator[](std::size_t pos) const
{
// 注意: C++17 起需要 std::launder
return *reinterpret_cast(data+pos);
}
// 从对齐存储删除对象
~static_vector()
{
for(std::size_t pos = 0; pos < m_size; ++pos) {
// 注意: C++17 起需要 std::launder
reinterpret_cast(data+pos)->~T();
}
}
};
int main()
{
static_vector v1;
v1.emplace_back(5, '*');
v1.emplace_back(10, '*');
std::cout << v1[0] << '\n' << v1[1] << '\n';
}
运行结果:
***** **********
2. alignas
示例代码:
// sse_t 类型的每个对象将对齐到 16 字节边界
struct alignas(16) sse_t
{
float sse_data[4];
};
// 数组 "cacheline" 将对齐到 128字节边界
alignas(128) char cacheline[128];
int main()
{
}
3. alignof
示例代码:
#include
struct Foo {
int i;
float f;
char c;
};
struct Empty {};
struct alignas(64) Empty64 {};
int main()
{
std::cout << "Alignment of" "\n"
"- char : " << alignof(char) << "\n"
"- pointer : " << alignof(int*) << "\n"
"- class Foo : " << alignof(Foo) << "\n"
"- empty class : " << alignof(Empty) << "\n"
"- alignas(64) Empty: " << alignof(Empty64) << "\n";
}
运行结果:
Alignment of - char : 1 - pointer : 8 - class Foo : 4 - empty class : 1 - alignas(64) Empty: 64
reference:
https://zh.cppreference.com/w/cpp/types/aligned_storage
https://zh.cppreference.com/w/cpp/language/alignas
https://zh.cppreference.com/w/cpp/language/alignof