1. alignas

设置类和struct的字节对齐方式

默认取值是:  2n : 0, 1, 2, 4 , 6, 8.....

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

2. alignof

区分sizeof(), alignof得到字节对齐的字节数

#include <iostream>
using namespace std;


//对结构体或类进行表示, 设置对齐方式为8字节
struct alignas(8) S {};

//struct alignas(1) U { S s; }  // warning

//有字节对齐(以4字节为对齐方式)
struct Foo
{
    int i;
    float f;
    char c;
};

struct Empty {};

struct alignas(64) Empty64 {};

struct alignas(1) Double {
    double d;
};

//以四字节为对齐方式, 即sizeof(Obj) == 8
struct Obj {
    char a;
    int b;
};


void alignInfo()
{
    cout << "sizeof(Obj)        : " << sizeof(Obj)      << endl; // 8
    cout << "alignof(Obj)       : " << alignof(Obj)     << endl; // 4
    cout << "sizeof(Foo)        : " << sizeof(Foo)      << endl; // 12
    cout << "sizeof(Double)     : " << sizeof(Double)   << endl; // 8
    cout << "sizeof(Empty64)    : " << sizeof(Empty64)  << endl; // 64

    cout << "\n";

    cout << "Alignment of " << endl;
    cout << "- char             : " << alignof(char)    << endl; // 1
    cout << "- pointer          : " << alignof(int*)    << endl; // 8
    cout << "- class Foo        : " << alignof(Foo)     << endl; // 4
    cout << "- empty class      : " << alignof(Empty)   << endl; // 1
    cout << "- alignas(64) Empty: " << alignof(Empty64) << endl; // 64
    cout << "- alignas(1) Double: " << alignof(Double)  << endl; // 8
}


int main()
{
    alignInfo();

    return 0;
}

【关键字】c++11关键字 随笔 第1张

3. auto 

#include <iostream>
using namespace std;

double add(double a, double b)
{
    return a + b;
}

double get_fun(int a)
{
    return a;
}

void showAuto()
{
    int aa = 1 + 2;
    auto a = 1 + 2;
    cout << "type of a: " << typeid(a).name() << endl;

    auto b = add(1, 1.2);
    cout << "type of b: " << typeid(b).name() << endl;

    auto c = {1, 2};
    cout << "type of c: " << typeid(c).name() << endl;

    auto my_lambda = [](int x) { return x + 3; };
    std::cout << "my_lambda: " << my_lambda(5) << endl;

    auto my_fun = get_fun(2);
    cout << "type of my_fun: " << typeid(my_fun).name() << endl;
    cout << "my_fun: " << get_fun(3) << endl;

}

int main()
{

    showAuto();
    return 0;
}

【关键字】c++11关键字 随笔 第2张

4. bitand 和 bitor 

#include <iostream>              
using namespace std;             
                                 
void showBitAndOr()              
{                                
    auto a = 3L;                 
    auto b = 4;                  
                                 
    //long                       
    auto c = a bitand b;  // &   
    auto d = a bitor b;   // |   
                                 
    cout << c << endl;           
                                 
    cout << d << endl;           
}                                
                                 
                                 
int main()                       
{                                
    showBitAndOr();              
                                 
    return 0;                    
}                                

5. constexpr: 常量表达式

  • 用于编译时的常量与常量函数。
  • 声明为constexpr函数的意义是:如果其参数均为合适的编译期常量,则对这个constexpr函数的调用就可用于 期望常量表达式 的场合(如模板的非类型参数,或枚举常量的值)。
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

int fact(int n)
{
    return n < 1 ? 1 : (n * fact(n - 1));
}

//编译器在编译器时, 就求出来返回的值了
constexpr int factorial(int n)
{
    return n <= 1 ? 1 : (n * factorial(n - 1));
}

template<int N>
struct NN {
    void print()
    {
        cout << N << endl;
    }
};

int main(int argc, char *argv[])
{

    //在编译器编译时调用的
    if (argc > 1) factorial(atoi(argv[1]));


    auto aa = fact(4);
    auto bb = factorial(2);
    char group[factorial(3)];   //编译器在编译时, 就求出来了

    NN<factorial(3)> nn;
    nn.print();

    return 0;

}

输出: 6

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄