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

posted @ 2019-02-26 19:05  我是好人007  阅读(1325)  评论(0)    收藏  举报