首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::alignment_of

Defined in header <type_traits>

template< class T > struct alignment_of;

(since C++11)

提供成员常量。value等于对齐要求类型T,好像是由alignof表情。如果T是数组类型,则返回元素类型的对齐要求。如果T是引用类型,则返回所引用类型的对齐要求。

如果alignof(T)不是有效的表达式,行为是未定义的。

辅助变量模板

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

(since C++17)

继承自STD:积分[医]常量

成员常数

value static

alignof(T) (public static member constant)

成员函数

operator std::size_t

converts the object to std::size_t, returns value (public member function)

operator() (C++14)

returns value (public member function)

成员类型

Type

Definition

value_type

std::size_t

type

std::integral_constant<std::size_t, value>

可能的实施

模板<class T>结构对齐[医]编号:STD::整数[医]常数<std::size[医]t,对齐%28t%29>{};

*。

注记

此类型特征早于关键字对齐,它可以用来获得相同的值,而不需要太多的冗长。

二次

代码语言:javascript
复制
#include <iostream>
#include <type_traits>
 
class A {};
 
int main() 
{
    std::cout << std::alignment_of<A>::value << '\n';
    std::cout << std::alignment_of<int>() << '\n'; // alt syntax
    std::cout << std::alignment_of_v<double> << '\n'; // c++17 alt syntax
}

二次

产出:

二次

代码语言:javascript
复制
1
4
8

二次

另见

alignof operator

queries alignment requirements of a type (since C++11)

aligned_storage (C++11)

defines the type suitable for use as uninitialized storage for types of given size (class template)

aligned_union (C++11)

defines the type suitable for use as uninitialized storage for all given types (class template)

max_align_t (C++11)

POD type with alignment requirement as great as any other scalar type (typedef)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券