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 不同

相关文章: