Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lmj01 committed Nov 3, 2023
1 parent d30a127 commit 905d631
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
26 changes: 23 additions & 3 deletions cpl/cplusplus.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@

## TMP(template metaprogramming)

可参考的内容

- [关于C++TMP比较新的解释,叫全面些](https://www.3dgep.com/beginning-cpp-template-programming/)

C++ allows template metaprogramming (TMP), a technique in which so-called templates are used by a compiler to generate temporary source code that is merged with the rest of the source code and then compiled

最初模板编程是指控制代码生成来实现泛型,现在的模板更多指泛型约束、编译期计算、对类型做计算。

**代码生成**
### **代码生成**

C语言使用宏来实现泛型,原理就是替换宏参数,属于编译期的字符串拼接
C语言没有模板是因为其语言抽象能力不够,相比C++的抽象能力模板template来实现泛型,宏的缺点都解决掉了。

**对类型约束**
### **对类型约束**

函数重载是代码生成的一种方案,template就是先处理函数重载和隐式类型转换上花掉很多时间,报错也是一大推的根本原因。

Expand Down Expand Up @@ -48,7 +52,7 @@ void print2(T x) {
```
其他语言如C#和rust,对泛型的约束是通过where来表达的。
**编译期计算**
### **编译期计算**
- 编译器对常量表达式的优化,如C函数的strlen,在C++中strlen是constexpr的
- 为减少运行时开销,会提前算好一些数据,典型的例子是计算三角函数表这种常量表
Expand Down Expand Up @@ -104,5 +108,21 @@ int main() {
```
允许一个运行期函数前面直接加上constexpr关键字修饰,表示函数既可以在运行期调用,也可以在编译期调用.如果只想在编译期执行函数,使用关键字consteval来修饰.
### **对类型做计算与编译期常量**
在别的编程语言中类型大多是运行时的, C++允许在编译期对类型进行操作,但是类型不能作为普通值,只能作为模板参数。
使用std::variant时,需要从一个类型列表里(variant的模板参数)里面根据类型返回一个索引。
对类型做计算不得不用模板元编程,需要在编译期计算的同时实例化模板也得使用模板元编程。
把类型当成值的改动是完全不可接受的unacceptable的,这是静态语言的特性,而像rust这样是完全不支持的。但有取巧的方法,把类型映射到编译期确定的值,由于字符串是值,只对字符串进行计算。[C++23的typeid实现类似的映射,而不使用字符串映射。](https://godbolt.org/z/GK4KjqKMc)
一个较新的编程语言[zig](https://ziglang.org/),它解决了上述的问题,支持编译期的变量,还支持类型作为一等公民(type as value)进行操作,得益于zig独特的comptime机制,被它标记的变量或代码块都在编译期执行的。
### 额外其他
- 首先满足正确性而非效率性
在一些数据范围小答案可以枚举,且时间上较为苛刻的,使用暴力枚举得出答案,将答案写入数组中。是指先生成一些数据可直接使用,减少运行的时间,对计算量大的可以这样优化。别人把特定计算放在一些文件中,部署出去的程序就跑得飞快,其他人还好奇为什么你的就这么快。
12 changes: 12 additions & 0 deletions database/database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Database

## 开源的数据库
- mysql
- [OceanBase](https://github.com/oceanbase/oceanbase)

## 事务
ACID属性
- 原子性Atomicity
- 一致性Consistency
- 隔离性Isolation
- 持久性Durability
8 changes: 7 additions & 1 deletion index/community.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
- [CAF is an open source implementation of the actor model for C++ featuring lightweight & fast actor implementations, pattern matching for messages, network transparent messaging, and more.](https://www.actor-framework.org/)
- [document](https://actor-framework.readthedocs.io/en/stable/Introduction.html)

### Publish/Subscribe messaging paradigm
### Paradigm

[What,if anything, is a programming paradigm? Robert Harper Carnegie Mellon University April 2017](https://www.cs.cmu.edu/~rwh/papers/paradigms/paradigm.pdf)

[Programming paradigms for dummies: what every programmer should know](https://blog.acolyer.org/2019/01/25/programming-paradigms-for-dummies-what-every-programmer-should-know/)

#### Publish/Subscribe messaging paradigm

- [Publisher-Subscriber pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/publisher-subscriber)

Expand Down
1 change: 1 addition & 0 deletions index/computerScience.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

### Database

- [database](../database/database.md)
- [redis](../database/redis.md)
- [MySql](../database/mysql.md)
- [sql](../database/sql.md)
Expand Down

0 comments on commit 905d631

Please sign in to comment.