alignof, alignas

DongWook Lee·2024년 7월 18일

C++

목록 보기
8/18
#include <iostream>
 
struct Foo
{
    int   i;
    float f;
    char  c;
};
 
// Note: `alignas(alignof(long double))` below can be simplified to simply 
// `alignas(long double)` if desired.
struct alignas(alignof(long double)) Foo2
{
    // put your definition here
}; 
 
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"
        "- class Foo2      : " << alignof(Foo2)    << "\n"
        "- empty class     : " << alignof(Empty)   << "\n"
        "- empty class\n"
        "  with alignas(64): " << alignof(Empty64) << "\n";
}
Alignment of
- char            : 1
- pointer         : 8
- class Foo       : 4
- class Foo2      : 16
- empty class     : 1
- empty class
  with alignas(64): 64

출처: cppreference.com

0개의 댓글