c++关键词的简单说明

大纲

  • alignas
  • alignof
  • asm
  • constexpr
  • const_cast
  • decltype
  • default
  • explicit
  • export
  • extern
  • mutable
  • noexcept
  • register
  • reinterpret
  • thread_local
  • throw
  • typeid
  • virtual
  • volatile

alignas

对齐

class alignas(long long) A
{
    int a;
}
class A{
    char[9] c;
    alignas(16) int a;
}

alignof

int main()
{
    std::cout << "alignof(struct_float) = " << alignof(A) << std::endl;
}

asm

 __asm
{
    MOV AL, 2
    MOV DX, 0xD007
    OUT AL, DX
}

constexpr

constexpr修饰常量

const 只读

constexpr int func(int n)
{
    return 1 + n;
}

const int func1(int n)
{
    return 1 + n;
}

int main()
{
    array <int,func(4)> myarr{1,2,3,4,5}; // success
    array <int,func1(4)> myarr{1,2,3,4,5}; //error
    return 0;
}

const_cast

const_cast是去掉const

const int constant = 26;
const int* const_p = &constant;
int* modifier = const_cast<int*>(const_p);
*modifier = 3;

decltype

推导类型

int i = 4;
decltype(i) a; //推导结果为int。a的类型为int。
vector<int >vec;
typedef decltype(vec.begin()) vectype;
for (vectype i = vec.begin; i != vec.end(); i++){

}

default

  • explicit
  • export
  • extern
  • mutable
  • noexcept
  • register
  • reinterpret
  • thread_local
  • throw
  • typeid
  • virtual
  • volatile