C++11 からの alignof でアラインメントを取得2021年06月12日 11時40分56秒

C++11 から追加されたアラインメントを取得するオペレーター。オブジェクト指向で書くと、アラインメントまで細かく機にして変数を配置する事自体は減っている。それでも、明示的に言語体系としてアラインメントを取得できるのは嬉しい。
% cat alignof.cpp.html
// c++ -std=c++11
#include <iostream>

struct double_char
{
    double d;
    char i;
};

struct short_char
{
    short f;
    char i;
};

int main()
{
    std::cout << alignof( double_char ) << std::endl;
    std::cout << alignof( short_char ) << std::endl;
}
実行結果。
% c++ -std=c++11 alignof.cpp
% ./a.out 
4
2

次回