类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。
试图特化定义于
定义于
| value [静态] | alignof(T) (公开静态成员常量) |
| operator std::size_t | 转换对象为 std::size_t ,返回 value(公开成员函数) |
| operator() (C++14) | 返回 value(公开成员函数) |
| 类型 | 定义 |
value_type | std::size_t |
type | std::integral_constant |
std::alignment_of
| template< class T > | (C++11 起) |
提供等于 T 类型对齐要求的成员常量 value ,如同用 alignof 表达式获得。若 T 是数组类型,则返回元素类型的对齐要求,若 T 是引用类型,则返回备用用类型的对齐要求。
若 alignof(T) 不是合法表达式,则行为未定义。
| template< class T > | (C++17 起) |
- template< class T >
- struct alignment_of :
- std::integral_constant<std::size_t,alignof(T)> {};
此类型特性先于 alignof 关键词出现,该关键词能用于较简明地获得相同值。
- #include <iostream>
- #include <type_traits>
-
- class A {};
-
- class B
- {
- int b;
- };
-
- class C
- {
- int b;
- double c;
- };
-
- int main()
- {
- std::cout << "std::alignment_of
::value: " - << std::alignment_of<int>::value << std::endl;
- std::cout << "std::alignment_of
::value: " - << std::alignment_of<double>::value << std::endl;
- std::cout << "std::alignment_of
::value: " - << std::alignment_of<char>::value << std::endl;
- std::cout << "std::alignment_of
::value: " - << std::alignment_of<uint8_t>::value << std::endl;
- std::cout << "std::alignment_of
::value: " - << std::alignment_of<uint64_t>::value << std::endl;
- std::cout << "std::alignment_of
::value:" - << std::alignment_of<std::string>::value << std::endl;
- << std::alignment_of<A>::value << std::endl;
-
- << std::alignment_of<A>() << std::endl; // 另一种语法
- std::cout << "std::alignment_of(): "
- << std::alignment_of<B>() << std::endl; // 另一种语法
- std::cout << "std::alignment_of
(): " - << std::alignment_of<C>() << std::endl; // 另一种语法
-
- return 0;
- }
