/*
* c++11关键字
* alignas 设置对其方式
* alignof 对其方式大小
*/
#include <QCoreApplication>
#include <iostream>
using namespace std;
struct alignas(8) S //S至少是八个字节的对其方式
{
};
struct alignas(1) U //BAD
{
S s;
};
struct Foo
{
int i;
float f;
char c;
};
struct Empty
{
};
struct alignas(64) Empty64
{
};
struct alignas(1) Double
{
double d;
};
struct Obj
{
char a;
int b;
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
cout << sizeof(Obj) << " "<< alignof(Obj);
cout << "Alignment of " << "\n"
<< "-char " << alignof(char) << "\n"
<< "-pointer " << alignof(char*) << "\n"
<< "-Foo " << alignof(Foo) << "\n"
<< "-Empty " << alignof(Empty) << "\n"
<< "- alignas(64) Empty64 " << alignof(Empty64) << "\n"
<< "-alignas(1) Double " << alignof(Double) << "\n";
return a.exec();
}
网友评论