c++ 常量表达式
来源:互联网 发布:知乎hfp护肤品怎么样 编辑:程序博客网 时间:2024/06/06 05:50
const expression
任何表达式都有一种属性,类型。常量表达式即是表达式的一种类型。常量表达式指这种表达式能够在编译时刻被计算。
这种表达式,能被用到non-type模板参数,array sizes, 和其他需要const expression的地方。
int n = 1;std::array<int, n> a1; // error, n is not a constant expressionconst int cn = 2;std::array<int, cn> a2; // OK, cn is a constant expression
core const expression
core const expression 是指所有子表达式不满足以下条件的表达式:
调用一个没有声明为constexpr的函数:
constexpr int n = std::numeric_limits<int>::max(); // OK, max() is constexpr constexpr int m = std::time(NULL); // Error: std::time() is not constexpr
- 调用一个声明为constexpr的函数,但是没有定义函数体:
调用一个声明为constexpr的函数,但是,没有生成常量表达式。
constexpr const int* addr(const int& ir) { return &ir; } static const int x = 5; constexpr const int* xp = addr(x); // OK constexpr const int* tp = addr(5); // error: &5 is not a constant expression
A function call to a constexpr constructor with arguments that do not produce constant expressions in member-initializer lists that are called from this function
int x;struct A { constexpr A(bool b) : m(b?42:x) { } int m;};constexpr int v = A(true).m; // OKconstexpr int w = A(false).m; // error: non-const x
... ...
下面的情况是const expression(lvalue to rvalue转换 的表达式,大部分不是const expression):
有const限制符的int类型,并且用const expression 初始化
int main() { const std::size_t tabsize = 50; int tab[tabsize]; // OK: tabsize is a constant expression std::size_t n = 50; const std::size_t sz = n; int tab2[sz]; // error: sz is not a constant expression // because sz is not initialized with a constant expression }
... ...
0 0
- C 指针&&表达式&&常量
- [C++] constexpr和常量表达式
- 【C/C++】constexpr和常量表达式
- C 常量、变量、运算符和表达式
- LINUX C编程 二、常量、变量、表达式
- 常量和常量表达式
- 常量表达式
- 常量表达式
- Linux C语言程序设计(一)——常量、变量和表达式
- c++-->常量
- C常量
- 【c++】常量
- C常量
- c++(常量)
- C 常量
- 【C#】常量
- 表达式必须是常量表达式
- 字符串常量 表达式
- js判断当前访问客户端是ios或安卓或微信
- 用类描述坦克
- Ping++SDK接入
- 安卓在Activity中获取Fragment中的控件
- 工作中最全最常用的正则表达式
- c++ 常量表达式
- [绍棠] iOS开发- 文件共享(利用iTunes导入文件, 并且显示已有文件) 以及 iOS App与iTunes文件传输的方法和对iOS App文件结构的说明
- 孤儿进程与僵尸进程[总结](转)
- Navicat中常用到的实用工具
- 如何自定义验证器?
- Sublime Text 关闭自动更新提示
- abort()函数
- hibernate addEntity setResultTransformer
- 目标检测(1)