回答編集履歴
1
appendix
answer
CHANGED
@@ -16,4 +16,20 @@
|
|
16
16
|
```C++
|
17
17
|
auto& ad3 = f(vec);
|
18
18
|
```
|
19
|
-
`auto&`は左辺値参照型へと推論されます。ここでは`vector<string>&`に推論されます。
|
19
|
+
`auto&`は左辺値参照型へと推論されます。ここでは`vector<string>&`に推論されます。
|
20
|
+
|
21
|
+
---
|
22
|
+
|
23
|
+
本質問の発端になっていると邪推しますが、下記`auto&&`はその見た目に反して **右辺値参照型に推論されていません**。
|
24
|
+
|
25
|
+
```C++
|
26
|
+
auto && ad1 = f(vec);
|
27
|
+
```
|
28
|
+
|
29
|
+
この`auto&&`表記やテンプレート型パラメータ`T`を用いた`T&&`表記では、[Forwarding Reference](https://timsong-cpp.github.io/cppwp/n4659/temp.deduct#call-3) (俗称:[Universal Reference](https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers))と呼ばれる特殊な型推論が行われます。
|
30
|
+
|
31
|
+
型推論プロセスは次の通りです:
|
32
|
+
- `auto&&`は型パラメータ`T`を用いた`T&&`と同義です。
|
33
|
+
- 型パラメータ`T`は左辺値参照型`vector<string>&`に推論されます。
|
34
|
+
- `T&&`はいったん`vector<string>& &&`(左辺値参照型への右辺値参照型)と解釈されます。
|
35
|
+
- [Reference collapsing](https://timsong-cpp.github.io/cppwp/n4659/dcl.ref#6)とよばれるC++規則により、最終的には左辺値参照型`vector<string>&`となります。
|