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

alignof operator

查询类型的对齐要求。

句法

alignof( type-id )

?

?

返回类型的值。std::size_t...

解释

回报对齐类型的任何实例所需的字节数。类型id,它是完整类型、数组类型或引用类型。

如果类型是引用类型,则运算符将返回参考类型;如果类型是数组类型,则返回元素类型的对齐要求。

关键词

alignof...

注记

见对齐返回的值的含义和属性。alignof...

二次

代码语言:javascript
复制
#include <iostream>
 
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";
}

二次

可能的产出:

二次

代码语言:javascript
复制
Alignment of
- char             : 1
- pointer          : 8
- class Foo        : 4
- empty class      : 1
- alignas(64) Empty: 64

二次

另见

alignment requirement

restricts the addresses at which an object may be allocated

alignas specifier

specifies that the storage for the variable should be aligned by specific amount (C++11)

alignment_of (C++11)

obtains the type's alignment requirements (class template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com