正如标题所述,我需要某种方式来断言已使用 alignas 声明了一个类型:
struct alignas(16) MyStruct {
...
};
它旨在用于模板参数,其中模板类需要确保其模板化的类型是 16 字节对齐的。
您可以使用它alignof
来确保您获得的类型与正确的大小对齐。你会在你的函数中使用它,比如
template <typename T>
void foo(const T& bar)
{
static_assert(alignof(T) == 16, "T must have an alignment of 16");
// rest of function
}
如果在课堂上使用它,你会有
template <typename T>
class foo
{
static_assert(alignof(T) == 16, "T must have an alignment of 16");
// rest of class
};