Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from Ultcrt/cms/posts/2023-10-08-c-开发时遇到的一些难以排…
Browse files Browse the repository at this point in the history
…查的错误

Automatically generated. Merged on Decap CMS.
  • Loading branch information
Ultcrt authored Dec 25, 2023
2 parents 4d27798 + 15dd3a0 commit b64b777
Showing 1 changed file with 3 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ size_t 通常用作存储容器尺寸的变量,但是该类型为无符号类

## 模板超出最大递归限制
如果模板中定义了递归操作,需要额外注意模板递归的停止条件,例如下述代码:
```c++
```C++
template<int N>
int foo() {
if (N == 1) return 1;
Expand All @@ -62,7 +62,8 @@ int foo() {
上述代码想要利用模板求解1到N之和,但是编译器会给出模板实例化时超出限制的报错(`template instantiation depth exceeds maximum of 900`)。这是由于在实例化 `foo<N-1>` 时,没有给定编译器的停止条件。

`C++17` 之前,上述问题可以通过特化模板来解决,如下述代码所示:
```C++template<int N>
```C++
template<int N>
int foo() {
return N + foo<N-1>();
}
Expand Down

0 comments on commit b64b777

Please sign in to comment.