Skip to content

Commit 0788092

Browse files
committed
deploy: 75487b3
1 parent daa9ffd commit 0788092

6 files changed

Lines changed: 112 additions & 80 deletions

File tree

auto/index.html

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,28 +531,43 @@ <h3 id="_8">返回引用类型</h3>
531531
</blockquote>
532532
<p>这就是为什么 <code>int &amp;&amp;</code> 就只是右值引用,而 <code>auto &amp;&amp;</code> 以及 <code>T &amp;&amp;</code> 则会叫做万能引用。一旦允许前面的参数为 <code>auto</code> 或者模板参数,就可以代换,就可以实现左右通吃。</p>
533533
<h3 id="decltypeauto">真正的万能 <code>decltype(auto)</code></h3>
534-
<p>返回类型声明为 <code>decltype(auto)</code> 的效果等价于把返回类型替换为 <code>decltype((返回表达式))</code></p>
534+
<p>返回类型声明为 <code>decltype(auto)</code> 的效果等价于把返回类型替换为 <code>decltype(返回操作数)</code></p>
535535
<pre><code class="language-cpp">int i;
536536

537537
decltype(auto) func() {
538538
return i;
539539
}
540540
// 等价于:
541-
decltype((i)) func() {
541+
decltype(i) func() {
542542
return i;
543543
}
544544
// 等价于:
545-
int &amp;func() {
545+
int func() {
546546
return i;
547547
}
548548
</code></pre>
549+
<p>对于变量名,在 <code>return</code> 语句中加括号可以产生不同结果。</p>
550+
<pre><code class="language-cpp">int i;
551+
552+
decltype(auto) func() {
553+
return (i);
554+
}
555+
// 等价于:
556+
decltype((i)) func() {
557+
return (i);
558+
}
559+
// 等价于:
560+
int&amp; func() {
561+
return (i);
562+
}
563+
</code></pre>
549564
<blockquote>
550-
<p><img src="../img/warning.png" height="30px" width="auto" style="margin: 0; border: none"/> 注意 <code>decltype(i)</code><code>int</code><code>decltype((i))</code><code>int &amp;</code>。这是因为 <code>decltype</code> 实际上有两个版本!当 <code>decltype</code> 中的内容只是单独的一个标识符(变量名)时,会得到变量定义时的类型;而当 <code>decltype</code> 中的内容不是单纯的变量名,而是一个复杂的表达式时,就会进入 <code>decltype</code> 的第二个版本:表达式版,会求表达式的类型,例如当变量为 <code>int</code> 时,表达式 <code>(i)</code> 的类型是左值引用,<code>int &amp;</code>,而变量本身 <code>i</code> 的类型则是 <code>int</code>。此处加上 <code>()</code> 就是为了让 <code>decltype</code> 被迫进入“表达式”的那个版本,<code>decltype(auto)</code> 遵循的也是“表达式”这个版本的结果。</p>
565+
<p><img src="../img/warning.png" height="30px" width="auto" style="margin: 0; border: none"/> 注意 <code>decltype(i)</code><code>int</code><code>decltype((i))</code><code>int &amp;</code>。这是因为 <code>decltype</code> 实际上有两个版本!当 <code>decltype</code> 中的内容只是单独的一个标识符(变量名)时,会得到变量定义时的类型;而当 <code>decltype</code> 中的内容不是单纯的变量名,而是一个复杂的表达式时,就会进入 <code>decltype</code> 的第二个版本:表达式版,会求表达式的类型,例如当变量为 <code>int</code> 时,表达式 <code>(i)</code> 的类型是左值引用,<code>int &amp;</code>,而变量本身 <code>i</code> 的类型则是 <code>int</code>。此处加上 <code>()</code> 使得 <code>decltype</code> 进入“表达式”的那个版本,<code>decltype(auto)</code> 就会遵循“表达式”这个版本的结果。</p>
551566
</blockquote>
552567
<pre><code class="language-cpp">int i;
553568

554569
decltype(auto) func() {
555-
return i;
570+
return i + 1;
556571
}
557572
// 等价于:
558573
decltype((i + 1)) func() {

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ <h2 id="_1">前言</h2>
286286
<blockquote>
287287
<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>
288288
</blockquote>
289-
<p>更新时间:2025年03月29日 17:32:39 (UTC+08:00)</p>
289+
<p>更新时间:2025年08月15日 09:04:19 (UTC+08:00)</p>
290290
<p><a href="https://parallel101.github.io/cppguidebook">在 GitHub Pages 浏览本书</a> | <a href="https://142857.red/book">在小彭老师自己维护的镜像上浏览本书</a></p>
291291
<h2 id="_2">格式约定</h2>
292292
<blockquote>

js/print-site.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ Copy the table of contents from the sidebar's.
77
Only called when print-site-plugin option 'add_table_of_contents' is set to true
88
*/
99
function generate_toc() {
10-
const sidebar = document.body.getElementsByClassName("md-sidebar--secondary")[0];
10+
const sidebar = document.body.getElementsByClassName("md-sidebar--secondary")[0] ??
11+
document.getElementById("toc-collapse");
12+
1113
const sidebarToc = sidebar.getElementsByTagName("ul")[0];
1214

1315
var clonedToc = sidebarToc.cloneNode(true);

0 commit comments

Comments
 (0)