>>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 [蚤の市★]
- バリ島で男子生徒ら集団万引きか、防犯カメラ映像が拡散 京都の大谷中学・高校が「窃盗行為」謝罪★5 [七波羅探題★]
- 【速報】 米トランプ政権声明 「中国が台湾を奪おうとする、いかなる試みも阻止する」 中国「レッドラインだ」 ★2 [お断り★]
- 元プロ野球選手・堂上隼人(43)を20代女性2人へのわいせつ未遂容疑で8回目の逮捕…これまでの被害者は10代・20代の女性11人に [Anonymous★]
- 【高校野球】なぜ『7回制』は反対多数でも止まらないか… 高野連が「全員の命」守るために貫く伝統より改革の姿勢 [冬月記者★]
- 「残業キャンセル界隈」若者が増加?「職務放棄」との批判も…“定時退社の権利”どこまで通用するか [七波羅探題★]
- ネトウヨ「我が国の総理大臣可愛すぎるだろ!」 [201193242]
- 【高市悲報】自衛隊「実は事前に現場海域で中国軍から空母での発着訓練をすると通告がありました」え…?😨😨 [931948549]
- 【高市速報】中国「このままだと日本が先制攻撃してくる」 [583597859]
- AI「その画像AIだぞ」AIぼく「そうなんだ助かる」
- 【悲報】山里亮太(南海キャンディーズ)さん [329329848]
- 【なにここ】🏡👊😅👊🏡【すごいなこれ‼】
