std::alignment_of_C++中文网

template< class T >
struct alignment_of;

(C++11 起)

提供等于 T 类型对齐要求的成员常量 value ,如同用 alignof 表达式获得。若 T 是数组类型,则返回元素类型的对齐要求,若 T 是引用类型,则返回备用用类型的对齐要求。

alignof(T) 不是合法表达式,则行为未定义。

添加 alignment_of alignment_of_v (C++17 起) 的特化的程序行为未定义。

辅助变量模板

template< class T >
inline constexpr std::size_t alignment_of_v = alignment_of<T>::value;

(C++17 起)

可能的实现

注解

此类型特征早于 alignof 关键词出现,该关键词能用于较简明地获得相同值。

示例

#include <iostream>
#include <type_traits>
 
class A {};
 
int main() 
{
    std::cout << std::alignment_of<A>::value << '\n';
    std::cout << std::alignment_of<int>() << '\n'; // 另一种语法
    std::cout << std::alignment_of_v<double> << '\n'; // c++17 另一种语法
}

可能的输出:

参阅