C++ constexpr变量和constexpr函数

  constexpr 类型变量必须用常量表达式或 constexpr 函数来初始化:

  constexpr int a=10;  

  constexpr int b=a+10;  

  constexpr int c=d();  //当 d()为一个 constexpr 函数时才可以

  

  constexpr 函数的形参和返回值都只能是字面型类型,且只能有一条 return 语句:

  constexpr d() { return 5; }

  如果 constexpr 定义了一个指针,限定符只对指针有效,即为常量指针:

  constexpr int *p=0;  //p为常量指针,与const 不同

猜你喜欢

转载自www.cnblogs.com/wshr007/p/10439354.html