内联函数 和constexpr函数(C++)

内联函数声明用inline关键字

有些函数太简单了,为了免除转子函数再返回的开销

编译时在调用处用函数体进行替换,节省了参数传递、控制转移等开销
注意:

  • 内联函数体内不能有循环语句和switch语包
  • 内联函数的定义必须出现在内联函数第一次被调用之前
  • 对内联函数不能进行异常接口声明

constexpr函数

constexpr 修饰的函数在其所有参数都是constexpr时一定返回constexpr

constexpr函数举例

 constexpr int get size(){ return 20; }
constexpr int foo = get size();
//正确: foo是一一个常量表达式
 

发布了24 篇原创文章 · 获赞 7 · 访问量 3772

猜你喜欢

转载自blog.csdn.net/qq_39980334/article/details/104280975