File tree Expand file tree Collapse file tree 5 files changed +19
-7
lines changed Expand file tree Collapse file tree 5 files changed +19
-7
lines changed Original file line number Diff line number Diff line change @@ -321,9 +321,15 @@ <h2 id="_4">函数的返回值</h2>
321
321
}
322
322
</ code > </ pre >
323
323
< blockquote >
324
- < p > < img src ="../img/warning.png " height ="30px " width ="auto " style ="margin: 0; border: none "/> 对于返回类型不为 < code > void</ code > 的函数,必须写 < code > return</ code > 语句,如果漏写,会出现可怕的未定义行为 (undefined behaviour)。编译器不一定会报错,而是到运行时才出现崩溃等现象。建议 GCC 用户开启 < code > -Werror=return-type</ code > 让编译器在编译时就检测此类错误,MSVC 则是开启 < code > /we4716</ code > 。更多未定义行为可以看我们的< a href ="../undef/ "> 未定义行为列表</ a > 章节。
325
- < img src ="../img/question.png " height ="30px " width ="auto " style ="margin: 0; border: none "/> 但有两个例外:1. main 函数是特殊的可以不写 return 语句,默认会自动帮你 < code > return 0;</ code > 。2. 具有 co_return 或 co_await 的协程函数可以不写 return 语句。</ p >
324
+ < p > < img src ="../img/warning.png " height ="30px " width ="auto " style ="margin: 0; border: none "/> 对于返回类型不为 < code > void</ code > 的函数,必须写 < code > return</ code > 语句,如果漏写,会出现可怕的未定义行为 (undefined behaviour)。编译器不一定会报错,而是到运行时才出现崩溃等现象。建议 GCC 用户开启 < code > -Werror=return-type</ code > 让编译器在编译时就检测此类错误,MSVC 则是开启 < code > /we4716</ code > 。更多未定义行为可以看我们的< a href ="../undef/ "> 未定义行为列表</ a > 章节。</ p >
325
+ < p > < img src ="../img/question.png " height ="30px " width ="auto " style ="margin: 0; border: none "/> 但有两个例外:1. main 函数是特殊的可以不写 return 语句,默认会自动帮你 < code > return 0;</ code > 。2. 具有 co_return 或 co_await 的协程函数可以不写 return 语句。</ p >
326
326
</ blockquote >
327
+ < p > < code > void</ code > 只是说明该函数没有返回值,返回还是能返回的。相当于你出门跑了一趟,但是没有带任何东西回家,并不代表你无法回家死外面了。也有一种真无法返回的函数,一旦调用了,就“无期徒刑”直到死亡都无法离开:</ p >
328
+ < pre > < code class ="language-cpp "> [[noreturn]] void func() {
329
+ std::terminate();
330
+ }
331
+ </ code > </ pre >
332
+ < p > < code > std::terminate()</ code > 的效果是终止当前进程。而我们的函数< code > func</ code > 所有可能的分支都会走向< code > std::terminate()</ code > ,一旦调用了< code > std::terminate()</ code > ,程序就退出了,不可能再执行下面的语句,这种函数叫做“无返回函数”。C语言可以用< code > noreturn</ code > 关键字修饰,而现代C++可以用< code > [[noreturn]]</ code > 修饰,提醒编译器这是一个不可能正常返回的函数,从而帮助它优化和诊断你的程序。例如:< code > std::exit</ code > ,< code > throw</ code > 、< code > std::terminate</ code > 、< code > std::abort</ code > 、< code > while (1)</ code > 都会让函数变成无返回函数(除非有其他能正常返回的分支)。</ p >
327
333
< h3 id ="_5 "> 接住返回值</ h3 >
328
334
< h2 id ="_6 "> 函数的参数</ h2 >
329
335
< h3 id ="vs "> 形参 vs 实参</ h3 >
Original file line number Diff line number Diff line change @@ -292,7 +292,7 @@ <h2 id="_1">前言</h2>
292
292
< blockquote >
293
293
< p > < img src ="./img/bulb.png " height ="30px " width ="auto " style ="margin: 0; border: none "/> 本书还在持续更新中……要追番的话,可以在 < a href ="https://github.com/parallel101/cppguidebook "> GitHub</ a > 点一下右上角的 “Watch” 按钮,每当小彭老师提交新 commit,GitHub 会向你发送一封电子邮件,提醒你小彭老师更新了。</ p >
294
294
</ blockquote >
295
- < p > 更新时间:2024年11月14日 18:11:41 (UTC+08:00)</ p >
295
+ < p > 更新时间:2024年11月26日 12:26:35 (UTC+08:00)</ p >
296
296
< p > < a href ="https://parallel101.github.io/cppguidebook "> 在 GitHub Pages 浏览本书</ a > | < a href ="https://142857.red/book "> 在小彭老师自己维护的镜像上浏览本书</ a > </ p >
297
297
< h2 id ="_2 "> 格式约定</ h2 >
298
298
< blockquote >
Original file line number Diff line number Diff line change @@ -421,7 +421,7 @@ <h2 id="index-_1">前言</h2>
421
421
<blockquote>
422
422
<p><img src="../img/bulb.png" height="30px" width="auto" style="margin: 0; border: none"/> 本书还在持续更新中……要追番的话,可以在 <a href="https://github.com/parallel101/cppguidebook">GitHub</a> 点一下右上角的 “Watch” 按钮,每当小彭老师提交新 commit,GitHub 会向你发送一封电子邮件,提醒你小彭老师更新了。</p>
423
423
</blockquote>
424
- <p>更新时间:2024年11月14日 18:11:41 (UTC+08:00)</p>
424
+ <p>更新时间:2024年11月26日 12:26:35 (UTC+08:00)</p>
425
425
<p><a href="https://parallel101.github.io/cppguidebook">在 GitHub Pages 浏览本书</a> | <a href="https://142857.red/book">在小彭老师自己维护的镜像上浏览本书</a></p>
426
426
<h2 id="index-_2">格式约定</h2>
427
427
<blockquote>
@@ -917,9 +917,15 @@ <h2 id="functions-_4">函数的返回值</h2>
917
917
}
918
918
</code></pre>
919
919
<blockquote>
920
- <p><img src="../img/warning.png" height="30px" width="auto" style="margin: 0; border: none"/> 对于返回类型不为 <code>void</code> 的函数,必须写 <code>return</code> 语句,如果漏写,会出现可怕的未定义行为 (undefined behaviour)。编译器不一定会报错,而是到运行时才出现崩溃等现象。建议 GCC 用户开启 <code>-Werror=return-type</code> 让编译器在编译时就检测此类错误,MSVC 则是开启 <code>/we4716</code>。更多未定义行为可以看我们的<a href="#undef">未定义行为列表</a>章节。
921
- <img src="../img/question.png" height="30px" width="auto" style="margin: 0; border: none"/> 但有两个例外:1. main 函数是特殊的可以不写 return 语句,默认会自动帮你 <code>return 0;</code>。2. 具有 co_return 或 co_await 的协程函数可以不写 return 语句。</p>
920
+ <p><img src="../img/warning.png" height="30px" width="auto" style="margin: 0; border: none"/> 对于返回类型不为 <code>void</code> 的函数,必须写 <code>return</code> 语句,如果漏写,会出现可怕的未定义行为 (undefined behaviour)。编译器不一定会报错,而是到运行时才出现崩溃等现象。建议 GCC 用户开启 <code>-Werror=return-type</code> 让编译器在编译时就检测此类错误,MSVC 则是开启 <code>/we4716</code>。更多未定义行为可以看我们的<a href="#undef">未定义行为列表</a>章节。</p>
921
+ <p>< img src="../img/question.png" height="30px" width="auto" style="margin: 0; border: none"/> 但有两个例外:1. main 函数是特殊的可以不写 return 语句,默认会自动帮你 <code>return 0;</code>。2. 具有 co_return 或 co_await 的协程函数可以不写 return 语句。</p>
922
922
</blockquote>
923
+ <p><code>void</code>只是说明该函数没有返回值,返回还是能返回的。相当于你出门跑了一趟,但是没有带任何东西回家,并不代表你无法回家死外面了。也有一种真无法返回的函数,一旦调用了,就“无期徒刑”直到死亡都无法离开:</p>
924
+ <pre><code class="language-cpp">[[noreturn]] void func() {
925
+ std::terminate();
926
+ }
927
+ </code></pre>
928
+ <p><code>std::terminate()</code>的效果是终止当前进程。而我们的函数<code>func</code>所有可能的分支都会走向<code>std::terminate()</code>,一旦调用了<code>std::terminate()</code>,程序就退出了,不可能再执行下面的语句,这种函数叫做“无返回函数”。C语言可以用<code>noreturn</code>关键字修饰,而现代C++可以用<code>[[noreturn]]</code>修饰,提醒编译器这是一个不可能正常返回的函数,从而帮助它优化和诊断你的程序。例如:<code>std::exit</code>,<code>throw</code>、<code>std::terminate</code>、<code>std::abort</code>、<code>while (1)</code>都会让函数变成无返回函数(除非有其他能正常返回的分支)。</p>
923
929
<h3 id="functions-_5">接住返回值</h3>
924
930
<h2 id="functions-_6">函数的参数</h2>
925
931
<h3 id="functions-vs">形参 vs 实参</h3>
Load Diff Large diffs are not rendered by default.
You can’t perform that action at this time.
0 commit comments