>>142
template<typename T>
T&& std::forward(typename remove_reference<T>::type& t) noexcept; //§35.5.1
template<typename T>
T&& std::forward(typename remove_reference<T>::type&& t) noexcept;
template<typename TT, typename A>
unique_ptr<TT> make_unique(int i, A&& a) // simple variant of make_shared (§34.3.2)
{
return unique_ptr<TT>{new TT{i,forward<A>(a)}};
}
We want make_unique<T>(arg) to construct a T from an arg without making any spurious copies. To do that, it is essential that the lvalue/rvalue distinction is maintained. Consider: auto p1 = make_unique<Xref<string>>(7,"Here");
"Here" is an rvalue, so forward(string&&) is called, passing along an rvalue, so that Xref(int,string&&) is called to move from the string holding "Here". The more interesting (subtle) case is: auto p2 = make_unique<Xref<string>>(9,x); Here, x is an lvalue, so forward(string&) is called, passing along an lvalue: forward()’s T is deduced to string& so that the return value becomes string& &&, which means string& (§7.7.3). Thus, Xref(int,string&) is called for the lvalue x, so that x is copied.
Stroustrup, Bjarne. The C++ Programming Language (p.689). Pearson Education. Kindle 版.
探検
C++相談室 part162
■ このスレッドは過去ログ倉庫に格納されています
143デフォルトの名無しさん (オイコラミネオ MM71-ZKGJ)
2022/11/16(水) 00:38:41.12ID:g3O9ReAZM■ このスレッドは過去ログ倉庫に格納されています
ニュース
- 国内ホテル、既にキャンセルも 訪日客関連業界、事態見守る [蚤の市★]
- 高市総理の周辺「小さな火種が火事になりかけている。早く鎮火しないといけない」 ★4 [Hitzeschleier★]
- 【外交】日中関係悪化、長期化の様相 2012年には自動車輸出80%減も ロイター★2 [1ゲットロボ★]
- 早大名誉教授が高市早苗首相の発言に言及「台湾も迷惑だと言っている」… [BFU★]
- 高市総理の周辺「小さな火種が火事になりかけている。早く鎮火しないといけない」 ★5 [Hitzeschleier★]
- 「影響これから」不安募るインバウンド関連業界 中国の訪日自粛要請 [蚤の市★]
- 中国が6年かけて米国から輸入するヘリウム依存の脱却に成功 99%→5% 国内生産も開始 [633746646]
- 【んな専🏡】姫森ルーナ(・o・🍬)総合スレッド🏰【ホロライブ▶】
- 道路の白線ってなんでかすれていくの [787212328]
- 愛国者「台湾有事になったらこれだけの国が台湾側で参戦するぞ!」→6万いいね [834922174]
- 東亜板のネトウヨ絶望大発狂2スレ目🤣中国軍機関紙「日本が台湾情勢に武力介入すれば日本全土が戦場になる」 [718678614]
- 【実況】博衣こよりのえちえち雀魂1位耐久🧪★2
