■ このスレッドは過去ログ倉庫に格納されています
C++相談室 part162
- 150 :デフォルトの名無しさん :2022/11/16(水) 00:50:02.83 ID:g3O9ReAZM.net
- >>143
142と143の間に、次の記述が挟まっています。
So:
string x {"There and back again"};
Xref<string> r1 {7,"Here"}; // r1 owns a copy of string{"Here"}
Xref<string> r2 {9,x}; // r2 just refers to x
Xref<string> r3 {3,new string{"There"}}; // r3 owns the string{"There"}
Here, r1 picks Xref(int,string&&) because x is an rvalue.
Similarly, r2 picks Xref(int,string&) because x is an lvalue.
Lvalues and rvalues are distinguished by template argument deduction:
an lvalue of type X is deduced as an X& and an rvalue as X.
This differs from the binding of values to non-template argument rvalue references (§12.2.1)
but is especially useful for argument forwarding (§35.5.1).
Consider writing a factory function that make Xrefs on the free store and returns
unique_ptrs to them:
総レス数 1001
307 KB
新着レスの表示
掲示板に戻る
全部
前100
次100
最新50
read.cgi ver 2014.07.20.01.SC 2014/07/20 D ★