Topic: Uniform intialization with


Author: Ville Voutilainen <ville.voutilainen@gmail.com>
Date: Thu, 29 Aug 2013 08:56:31 +0300
Raw View
--001a11c2c97c8a61ef04e50fc461
Content-Type: text/plain; charset=ISO-8859-1

On 23 August 2013 20:14, <inkwizytoryankes@gmail.com> wrote:

>
>
> On Wednesday, August 14, 2013 12:50:47 PM UTC+2, toma...@gmail.com wrote:
>>
>> The std::initializer_list<T> is defined as a non-owning handle to a const
>> array of the T elements placed probably on stack, with requires from
>>  containers constructor to copy the elements into container. This lead
>> for usability problem, especially in generic programming.
>>
>> The most common example of usage of std::initializer_list is to write:
>>   std::vector<int> v{1,2,3,4};
>> instead of:
>>   std::vector<int> v; v.reserve(4);
>>   v.emplace_back(1);
>>   v.emplace_back(2);
>>   v.emplace_back(3);
>>   v.emplace_back(4);
>> There is not difference between this two codes, because the copy and move
>> constructor of int does not differ.
>>
>> It can be easy fixed by helper class:
>
> template<typename T>
> class make_vector
> {
>     std::vector<T> data;
> public:
>     template<typename... A>
>     inline make_vector(A&&... a)
>     {
>         data.reserve(sizeof...(A));
>         auto f = {(data.emplace_back(std::forward<A>(a)), 0)...}; //hack
> to reduce code size
>     }
>
>     operator std::vector<T>() { return std::move(data); }
> };
>
> int main()
> {
>     std::vector<std::unique_ptr<int>> v = make_vector<std::unique_ptr<int>
> >
>     {
>         std::unique_ptr<int>(new int{1}),
>         std::unique_ptr<int>(new int{2}),
>         std::unique_ptr<int>(new int{4}),
>     };
>     for(auto& i : v)
>         std::cout<< *i << "\n";
> }
>
>
So.. the problem at hand is that people claim that I can't move from an
initializer_list of move-only type.
If I have a vector<unique_ptr<int>> member, I have to resort to wrapper
types to have a ctor-initializer
initialize the member.
Hogwash, I say! Lambdas to the rescue!

struct X {
    vector<unique_ptr<int>> vup;
    X(unique_ptr<int> src)  // brace yourselves (hmm. no pun intended.),
here we gooooo.....
        : vup{
                 [](unique_ptr<int> src){
                     vector<unique_ptr<int>> tmp;
                     tmp.push_back(move(src));
                     return tmp;
                 }(move(src)),
         }
    {
    }
};

int main()
{
    X x{unique_ptr<int>{new int{}}};
}

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

--001a11c2c97c8a61ef04e50fc461
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><div class=3D"gmail_extra"><br><br><div class=3D"gmail=
_quote">On 23 August 2013 20:14,  <span dir=3D"ltr">&lt;<a href=3D"mailto:i=
nkwizytoryankes@gmail.com" target=3D"_blank">inkwizytoryankes@gmail.com</a>=
&gt;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><div cla=
ss=3D"im"><br><br>On Wednesday, August 14, 2013 12:50:47 PM UTC+2, <a href=
=3D"mailto:toma...@gmail.com" target=3D"_blank">toma...@gmail.com</a> wrote=
:<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border=
-left:1px solid rgb(204,204,204);padding-left:1ex">
<div dir=3D"ltr">The std::initializer_list&lt;T&gt; is defined as a non-own=
ing handle to a const array of the T elements placed probably on stack, wit=
h requires from<br>=A0containers constructor to copy the elements into cont=
ainer. This lead for usability problem, especially in generic programming.<=
br>
<br>The most common example of usage of std::initializer_list is to write:<=
br><span style=3D"font-family:courier new,monospace">=A0 std::vector&lt;int=
&gt; v{1,2,3,4};</span><br>instead of:<br><span style=3D"font-family:courie=
r new,monospace">=A0 std::vector&lt;int&gt; v; v.reserve(4);<br>
=A0 v.emplace_back(1);<br>=A0 v.emplace_back(2);<br>=A0 v.emplace_back(3);<=
br>=A0 v.emplace_back(4);</span><br>There is not difference between this tw=
o codes, because the copy and move constructor of int does not differ.<br><=
br></div>
</blockquote></div><div>It can be easy fixed by helper class:<br><div style=
=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-=
style:solid;border-width:1px;word-wrap:break-word"><code><div><span style><=
br>
</span><span style=3D"color:rgb(0,0,136)">template</span><span style=3D"col=
or:rgb(102,102,0)">&lt;</span><span style=3D"color:rgb(0,0,136)">typename</=
span><span style> T</span><span style=3D"color:rgb(102,102,0)">&gt;</span><=
span style><br>
</span><span style=3D"color:rgb(0,0,136)">class</span><span style> make_vec=
tor<br></span><span style=3D"color:rgb(102,102,0)">{</span><span style><br>=
=A0 =A0 std</span><span style=3D"color:rgb(102,102,0)">::</span><span style=
>vector</span><span style=3D"color:rgb(102,102,0)">&lt;</span><span style>T=
</span><span style=3D"color:rgb(102,102,0)">&gt;</span><span style> data</s=
pan><span style=3D"color:rgb(102,102,0)">;</span><span style><br>
</span><span style=3D"color:rgb(0,0,136)">public</span><span style=3D"color=
:rgb(102,102,0)">:</span><span style><br>=A0 =A0 </span><span style=3D"colo=
r:rgb(0,0,136)">template</span><span style=3D"color:rgb(102,102,0)">&lt;</s=
pan><span style=3D"color:rgb(0,0,136)">typename</span><span style=3D"color:=
rgb(102,102,0)">...</span><span style> A</span><span style=3D"color:rgb(102=
,102,0)">&gt;</span><span style><br>
=A0 =A0 </span><span style=3D"color:rgb(0,0,136)">inline</span><span style>=
 make_vector</span><span style=3D"color:rgb(102,102,0)">(</span><span style=
>A</span><span style=3D"color:rgb(102,102,0)">&amp;&amp;...</span><span sty=
le> a</span><span style=3D"color:rgb(102,102,0)">)</span><span style><br>
=A0 =A0 </span><span style=3D"color:rgb(102,102,0)">{</span><span style><br=
>=A0 =A0 =A0 =A0 data</span><span style=3D"color:rgb(102,102,0)">.</span><s=
pan style>reserve</span><span style=3D"color:rgb(102,102,0)">(</span><span =
style=3D"color:rgb(0,0,136)">sizeof</span><span style=3D"color:rgb(102,102,=
0)">...(</span><span style>A</span><span style=3D"color:rgb(102,102,0)">));=
</span><span style><br>
=A0 =A0 =A0 =A0 </span><span style=3D"color:rgb(0,0,136)">auto</span><span =
style> f </span><span style=3D"color:rgb(102,102,0)">=3D</span><span style>=
 </span><span style=3D"color:rgb(102,102,0)">{(</span><span style>data</spa=
n><span style=3D"color:rgb(102,102,0)">.</span><span style>emplace_back</sp=
an><span style=3D"color:rgb(102,102,0)">(</span><span style>std</span><span=
 style=3D"color:rgb(102,102,0)">::</span><span style>forward</span><span st=
yle=3D"color:rgb(102,102,0)">&lt;</span><span style>A</span><span style=3D"=
color:rgb(102,102,0)">&gt;(</span><span style>a</span><span style=3D"color:=
rgb(102,102,0)">)),</span><span style> </span><span style=3D"color:rgb(0,10=
2,102)">0</span><span style=3D"color:rgb(102,102,0)">)...};</span><span sty=
le> </span><span style=3D"color:rgb(136,0,0)">//hack to reduce code size</s=
pan><span style><br>
=A0 =A0 </span><span style=3D"color:rgb(102,102,0)">}</span><span style><br=
>=A0 =A0 <br>=A0 =A0 </span><span style=3D"color:rgb(0,0,136)">operator</sp=
an><span style> std</span><span style=3D"color:rgb(102,102,0)">::</span><sp=
an style>vector</span><span style=3D"color:rgb(102,102,0)">&lt;</span><span=
 style>T</span><span style=3D"color:rgb(102,102,0)">&gt;()</span><span styl=
e> </span><span style=3D"color:rgb(102,102,0)">{</span><span style> </span>=
<span style=3D"color:rgb(0,0,136)">return</span><span style> std</span><spa=
n style=3D"color:rgb(102,102,0)">::</span><span style>move</span><span styl=
e=3D"color:rgb(102,102,0)">(</span><span style>data</span><span style=3D"co=
lor:rgb(102,102,0)">);</span><span style> </span><span style=3D"color:rgb(1=
02,102,0)">}</span><span style><br>
</span><span style=3D"color:rgb(102,102,0)">};</span><span style><br><br></=
span><span style=3D"color:rgb(0,0,136)">int</span><span style> main</span><=
span style=3D"color:rgb(102,102,0)">()</span><span style><br></span><span s=
tyle=3D"color:rgb(102,102,0)">{</span><span style><br>
=A0 =A0 std</span><span style=3D"color:rgb(102,102,0)">::</span><span style=
>vector</span><span style=3D"color:rgb(102,102,0)">&lt;</span><span style>s=
td</span><span style=3D"color:rgb(102,102,0)">::</span><span style>unique_p=
tr</span><span style=3D"color:rgb(0,136,0)">&lt;int&gt;</span><span style=
=3D"color:rgb(102,102,0)">&gt;</span><span style> v </span><span style=3D"c=
olor:rgb(102,102,0)">=3D</span><span style> make_vector</span><span style=
=3D"color:rgb(102,102,0)">&lt;</span><span style>std</span><span style=3D"c=
olor:rgb(102,102,0)">::</span><span style>unique_ptr</span><span style=3D"c=
olor:rgb(0,136,0)">&lt;int&gt;</span><span style=3D"color:rgb(102,102,0)">&=
gt;</span><span style><br>
=A0 =A0 </span><span style=3D"color:rgb(102,102,0)">{</span><span style><br=
>=A0 =A0 =A0 =A0 std</span><span style=3D"color:rgb(102,102,0)">::</span><s=
pan style>unique_ptr</span><span style=3D"color:rgb(0,136,0)">&lt;int&gt;</=
span><span style=3D"color:rgb(102,102,0)">(</span><span style=3D"color:rgb(=
0,0,136)">new</span><span style> </span><span style=3D"color:rgb(0,0,136)">=
int</span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color=
:rgb(0,102,102)">1</span><span style=3D"color:rgb(102,102,0)">}),</span><sp=
an style><br>
=A0 =A0 =A0 =A0 std</span><span style=3D"color:rgb(102,102,0)">::</span><sp=
an style>unique_ptr</span><span style=3D"color:rgb(0,136,0)">&lt;int&gt;</s=
pan><span style=3D"color:rgb(102,102,0)">(</span><span style=3D"color:rgb(0=
,0,136)">new</span><span style> </span><span style=3D"color:rgb(0,0,136)">i=
nt</span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:=
rgb(0,102,102)">2</span><span style=3D"color:rgb(102,102,0)">}),</span><spa=
n style><br>
=A0 =A0 =A0 =A0 std</span><span style=3D"color:rgb(102,102,0)">::</span><sp=
an style>unique_ptr</span><span style=3D"color:rgb(0,136,0)">&lt;int&gt;</s=
pan><span style=3D"color:rgb(102,102,0)">(</span><span style=3D"color:rgb(0=
,0,136)">new</span><span style> </span><span style=3D"color:rgb(0,0,136)">i=
nt</span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:=
rgb(0,102,102)">4</span><span style=3D"color:rgb(102,102,0)">}),</span><spa=
n style><br>
=A0 =A0 </span><span style=3D"color:rgb(102,102,0)">};</span><span style><b=
r>=A0 =A0 </span><span style=3D"color:rgb(0,0,136)">for</span><span style=
=3D"color:rgb(102,102,0)">(</span><span style=3D"color:rgb(0,0,136)">auto</=
span><span style=3D"color:rgb(102,102,0)">&amp;</span><span style> i </span=
><span style=3D"color:rgb(102,102,0)">:</span><span style> v</span><span st=
yle=3D"color:rgb(102,102,0)">)</span><span style><br>
=A0 =A0 =A0 =A0 std</span><span style=3D"color:rgb(102,102,0)">::</span><sp=
an style>cout</span><span style=3D"color:rgb(102,102,0)">&lt;&lt;</span><sp=
an style> </span><span style=3D"color:rgb(102,102,0)">*</span><span style>i=
 </span><span style=3D"color:rgb(102,102,0)">&lt;&lt;</span><span style> </=
span><span style=3D"color:rgb(0,136,0)">&quot;\n&quot;</span><span style=3D=
"color:rgb(102,102,0)">;</span><span style><br>
</span><span style=3D"color:rgb(102,102,0)">}</span><span style><br></span>=
</div></code></div><br></div></div></blockquote><div><br></div><div>So.. th=
e problem at hand is that people claim that I can&#39;t move from an initia=
lizer_list of move-only type.<br>
</div><div>If I have a vector&lt;unique_ptr&lt;int&gt;&gt; member, I have t=
o resort to wrapper types to have a ctor-initializer<br>initialize the memb=
er.<br></div><div>Hogwash, I say! Lambdas to the rescue! <br><br>struct X {=
<br>
=A0=A0=A0 vector&lt;unique_ptr&lt;int&gt;&gt; vup; <br>=A0=A0=A0 X(unique_p=
tr&lt;int&gt; src)=A0 // brace yourselves (hmm. no pun intended.), here we =
gooooo.....<br>=A0=A0=A0=A0=A0=A0=A0 : vup{<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0 [](unique_ptr&lt;int&gt; src){<br>=A0=A0=A0=A0=A0=A0=
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 vector&lt;unique_ptr&lt;int&gt;&=
gt; tmp; <br>
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 tmp.push_back(=
move(src)); <br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0 return tmp;<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }(move(=
src)),<br>=A0=A0=A0=A0=A0=A0=A0=A0 } <br>=A0=A0=A0 {<br>=A0=A0=A0 }<br>}; <=
br><br>int main() <br>{<br>=A0=A0=A0 X x{unique_ptr&lt;int&gt;{new int{}}};=
<br>
}<br></div></div></div></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--001a11c2c97c8a61ef04e50fc461--

.