This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 4 元编程下的数据结构与算法 | |
### 4.1 表达式与数值计算 | |
#### 4.1.1 const与常量表达式 | |
我们先从大家都熟悉的const开始来介绍常量表达式,总所周知const是用于修饰一个常量的,我们声明一个普通的const 常量就必须给予初始化。 | |
而所谓的常量表达式,就是定义能在编译时求值的表达式。 | |
比如: | |
```C++ | |
//为了方便期间,我们用一个简单的类模板来观察一个表达式是否是常量表达式 | |
template<int x> | |
struct check{}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_+_::=<+|+> | |
0<+::=<+0? | |
1<+::=<+1? | |
2<+::=<+2? | |
3<+::=<+3? | |
4<+::=<+4? | |
5<+::=<+5? | |
6<+::=<+6? | |
7<+::=<+7? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template<int item> struct num { static int const value = item; };//包装整数 | |
struct null {}; //TODO, 空是否需要内容 | |
//-------------定义序对-------------- | |
template<class left, class right> | |
struct cons { | |
using car = left; | |
using cdr = right; | |
static int const length = cdr::length + 1; | |
}; |