Topic: std::swap for std::reference_wrapper


Author: Vlad from Moscow <vlad.moscow@mail.ru>
Date: Mon, 8 Apr 2013 12:57:08 -0700 (PDT)
Raw View
------=_Part_350_2665715.1365451028740
Content-Type: text/plain; charset=ISO-8859-1

In my opinion there is more sense to define std::swap (or member function
swap of std::reference_wrapper) such a way that it would swap the objects
referenced by std::reference_wrapper(s)  instead of
std::reference_wrapper(s) themselves.

For example.

template <class T>
void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b )
{
 std::swap<typename std::reference_wrapper<T>::type>( a, b );
}
That is instead of swapping std::reference_wrapper(s) to swap objects that
are referenced. For example

#include <iostream>
#include <functional>
template <class T>
void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b )
{
 std::swap<typename std::reference_wrapper<T>::type>( a, b );
}

int main()
{
  int x = 10, y = 20;
  auto rx = std::ref( x );
  auto ry = std::ref( y );
  std::cout << "x = " << x
         << ", y = " << y
      << ", rx = " << rx
      << ", ry = " << ry << std::endl;
  std::cout << std::endl;
  swap( rx, ry );
  std::cout << "x = " << x
         << ", y = " << y
      << ", rx = " << rx
      << ", ry = " << ry << std::endl;
  std::cout << std::endl;
 return 0;
}




--

---
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/?hl=en.



------=_Part_350_2665715.1365451028740
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>In my opinion there is more sense to define std::swap (or member funct=
ion swap of std::reference_wrapper) such a way that it would swap the objec=
ts referenced by std::reference_wrapper(s)&nbsp; instead of std::reference_=
wrapper(s) themselves.</div><div>&nbsp;</div><div>For example.</div><div><f=
ont size=3D"2" face=3D"Consolas"><font size=3D"2" face=3D"Consolas"></font>=
</font>&nbsp;</div><div><font size=3D"2" face=3D"Consolas"><font size=3D"2"=
 face=3D"Consolas">template &lt;class T&gt;<br>void swap( std::reference_wr=
apper&lt;T&gt; &amp;a, std::reference_wrapper&lt;T&gt; &amp;b )<br>{<br>&nb=
sp;std::swap&lt;typename std::reference_wrapper&lt;T&gt;::type&gt;( a, b );=
<br>}<br></font></font></div><div><font size=3D"2" face=3D"Consolas"><font =
size=3D"2" face=3D"Consolas">That is instead of swapping std::reference_wra=
pper(s)&nbsp;to swap objects that are referenced. For example</font></font>=
</div><div><font size=3D"2" face=3D"Consolas"><font size=3D"2" face=3D"Cons=
olas"></font></font>&nbsp;</div><div><font size=3D"2" face=3D"Consolas"><fo=
nt size=3D"2" face=3D"Consolas">#include &lt;iostream&gt;<br>#include &lt;f=
unctional&gt;</font></font></div><div><font size=3D"2" face=3D"Consolas"><f=
ont size=3D"2" face=3D"Consolas">template &lt;class T&gt;<br>void swap( std=
::reference_wrapper&lt;T&gt; &amp;a, std::reference_wrapper&lt;T&gt; &amp;b=
 )<br>{<br>&nbsp;std::swap&lt;typename std::reference_wrapper&lt;T&gt;::typ=
e&gt;( a, b );<br>}</font></font></div><font size=3D"2" face=3D"Consolas"><=
font size=3D"2" face=3D"Consolas"><div><br>int main()<br>{<br>&nbsp;&nbsp;i=
nt x =3D 10, y =3D 20;<br>&nbsp;&nbsp;auto rx =3D std::ref( x );<br>&nbsp;&=
nbsp;auto ry =3D std::ref( y );</div><div>&nbsp;&nbsp;std::cout &lt;&lt; "x=
 =3D " &lt;&lt; x <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;=
&lt; ", y =3D " &lt;&lt; y <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; ", r=
x =3D " &lt;&lt; rx <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; ", ry =3D "=
 &lt;&lt; ry &lt;&lt; std::endl;<br>&nbsp;&nbsp;std::cout &lt;&lt; std::end=
l;</div><div>&nbsp;&nbsp;swap( rx, ry );</div><div>&nbsp;&nbsp;std::cout &l=
t;&lt; "x =3D " &lt;&lt; x <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp; &lt;&lt; ", y =3D " &lt;&lt; y <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;=
&lt; ", rx =3D " &lt;&lt; rx <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; ",=
 ry =3D " &lt;&lt; ry &lt;&lt; std::endl;<br>&nbsp;&nbsp;std::cout &lt;&lt;=
 std::endl;<br></div><div>&nbsp;return 0;<br>}<br>&nbsp;</div><p>&nbsp;</p>
</font></font>

<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/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_350_2665715.1365451028740--

.


Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@gmail.com>
Date: Mon, 8 Apr 2013 22:15:20 +0200
Raw View
2013/4/8 Vlad from Moscow <vlad.moscow@mail.ru>:
> In my opinion there is more sense to define std::swap (or member function
> swap of std::reference_wrapper) such a way that it would swap the objects
> referenced by std::reference_wrapper(s)  instead of
> std::reference_wrapper(s) themselves.

Could you please make it a bit clearer why this change would be an
improvement? Currently I cannot agree with that proposal:
std::reference_wrapper is both CopyAssignable and CopyConstructible
and I find the current swap semantics quite natural.

> For example.
>
> template <class T>
> void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b )
> {
>  std::swap<typename std::reference_wrapper<T>::type>( a, b );
> }

Even if we would change the semantics of std::reference_wrapper I
would even more strongly disagree with this approach, because it
enforces the usage of the std::swap template even though the
underlying type might not be MoveAssignable or MoveConstructible (but
is swappable). It doesn't respect ADL-lookup for T, so it would behave
differently as any other library functions that swaps T objects.

> That is instead of swapping std::reference_wrapper(s) to swap objects that
> are referenced. For example
>
> #include <iostream>
> #include <functional>
> template <class T>
> void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b )
> {
>  std::swap<typename std::reference_wrapper<T>::type>( a, b );
> }
>
> int main()
> {
>   int x = 10, y = 20;
>   auto rx = std::ref( x );
>   auto ry = std::ref( y );
>   std::cout << "x = " << x
>          << ", y = " << y
>       << ", rx = " << rx
>       << ", ry = " << ry << std::endl;
>   std::cout << std::endl;
>   swap( rx, ry );
>   std::cout << "x = " << x
>          << ", y = " << y
>       << ", rx = " << rx
>       << ", ry = " << ry << std::endl;
>   std::cout << std::endl;
>  return 0;
> }

These example don't convince me: std::reference_wrapper objects are
typically used as an intermediate vehicle for a T reference. Except
for the function call operator overload it doesn't provide any
T-specific functionality (I ignore the conversion functions to T,
because they only mean that you can easily get the referenced T). It
seems IMO better to me to leave the swap semantics of
reference_wrapper as it currently is, because it perfectly matches the
existing copy-semantics, which is pointer-like. This is a well-defined
view of T to me. If you don't like the swap-semantics of this wrapper
type, you should convert it to the underling T reference and swap that
one.

- Daniel

--

---
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/?hl=en.



.


Author: Vlad from Moscow <vlad.moscow@mail.ru>
Date: Mon, 8 Apr 2013 13:57:04 -0700 (PDT)
Raw View
------=_Part_1124_4016898.1365454625061
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable


On Tuesday, April 9, 2013 12:15:20 AM UTC+4, Daniel Kr=FCgler wrote:
>
> 2013/4/8 Vlad from Moscow <vlad....@mail.ru <javascript:>>:=20
> > In my opinion there is more sense to define std::swap (or member=20
> function=20
> > swap of std::reference_wrapper) such a way that it would swap the=20
> objects=20
> > referenced by std::reference_wrapper(s)  instead of=20
> > std::reference_wrapper(s) themselves.=20
>
> Could you please make it a bit clearer why this change would be an=20
> improvement? Currently I cannot agree with that proposal:=20
> std::reference_wrapper is both CopyAssignable and CopyConstructible=20
> and I find the current swap semantics quite natural.=20
>
> > For example.=20
> >=20
> > template <class T>=20
> > void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b )=
=20
> > {=20
> >  std::swap<typename std::reference_wrapper<T>::type>( a, b );=20
> > }=20
>
> Even if we would change the semantics of std::reference_wrapper I=20
> would even more strongly disagree with this approach, because it=20
> enforces the usage of the std::swap template even though the=20
> underlying type might not be MoveAssignable or MoveConstructible (but=20
> is swappable). It doesn't respect ADL-lookup for T, so it would behave=20
> differently as any other library functions that swaps T objects.=20
>
> > That is instead of swapping std::reference_wrapper(s) to swap objects=
=20
> that=20
> > are referenced. For example=20
> >=20
> > #include <iostream>=20
> > #include <functional>=20
> > template <class T>=20
> > void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b )=
=20
> > {=20
> >  std::swap<typename std::reference_wrapper<T>::type>( a, b );=20
> > }=20
> >=20
> > int main()=20
> > {=20
> >   int x =3D 10, y =3D 20;=20
> >   auto rx =3D std::ref( x );=20
> >   auto ry =3D std::ref( y );=20
> >   std::cout << "x =3D " << x=20
> >          << ", y =3D " << y=20
> >       << ", rx =3D " << rx=20
> >       << ", ry =3D " << ry << std::endl;=20
> >   std::cout << std::endl;=20
> >   swap( rx, ry );=20
> >   std::cout << "x =3D " << x=20
> >          << ", y =3D " << y=20
> >       << ", rx =3D " << rx=20
> >       << ", ry =3D " << ry << std::endl;=20
> >   std::cout << std::endl;=20
> >  return 0;=20
> > }=20
>
> These example don't convince me: std::reference_wrapper objects are=20
> typically used as an intermediate vehicle for a T reference. Except=20
> for the function call operator overload it doesn't provide any=20
> T-specific functionality (I ignore the conversion functions to T,=20
> because they only mean that you can easily get the referenced T). It=20
> seems IMO better to me to leave the swap semantics of=20
> reference_wrapper as it currently is, because it perfectly matches the=20
> existing copy-semantics, which is pointer-like. This is a well-defined=20
> view of T to me. If you don't like the swap-semantics of this wrapper=20
> type, you should convert it to the underling T reference and swap that=20
> one.=20
>
> - Daniel=20
>
=20
=20
Here is an example of the utility of the such swap
=20
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <iterator>
=20
=20
namespace std
{
void swap( std::reference_wrapper<int> &a, std::reference_wrapper<int> &b )
{
    swap<int>( a, b );
}
=20
}
=20
=20
int main()
{
=20
                int a[] =3D { 1, 5, 2, 6, 9, 3, 7, 8, 0 };
                std::vector<std::reference_wrapper<int>> v;
=20
                for ( int x : a ) std::cout << x << ' ';
                std::cout << std::endl;
=20
                std::copy_if( std::begin( a ), std::end( a ),=20
std::back_inserter( v ),
                        []( int x ) { return ( x % 2 ); } );
=20
                std::reverse( v.begin(), v.end() );
=20
                for ( int x : a ) std::cout << x << ' ';
                std::cout << std::endl;
=20
        return 0;
}=20

--=20

---=20
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 e=
mail 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-proposa=
ls/?hl=3Den.



------=_Part_1124_4016898.1365454625061
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br>On Tuesday, April 9, 2013 12:15:20 AM UTC+4, Daniel Kr=FCgler wrote:<bl=
ockquote style=3D"margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left=
-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: soli=
d;" class=3D"gmail_quote">2013/4/8 Vlad from Moscow &lt;<a href=3D"javascri=
pt:" target=3D"_blank" gdf-obfuscated-mailto=3D"oTfr9xDLGr0J">vlad....@mail=
..ru</a>&gt;:
<br>&gt; In my opinion there is more sense to define std::swap (or member f=
unction
<br>&gt; swap of std::reference_wrapper) such a way that it would swap the =
objects
<br>&gt; referenced by std::reference_wrapper(s) &nbsp;instead of
<br>&gt; std::reference_wrapper(s) themselves.
<br>
<br>Could you please make it a bit clearer why this change would be an
<br>improvement? Currently I cannot agree with that proposal:
<br>std::reference_wrapper is both CopyAssignable and CopyConstructible
<br>and I find the current swap semantics quite natural.
<br>
<br>&gt; For example.
<br>&gt;
<br>&gt; template &lt;class T&gt;
<br>&gt; void swap( std::reference_wrapper&lt;T&gt; &amp;a, std::reference_=
wrapper&lt;T&gt; &amp;b )
<br>&gt; {
<br>&gt; &nbsp;std::swap&lt;typename std::reference_wrapper&lt;T&gt;::<wbr>=
type&gt;( a, b );
<br>&gt; }
<br>
<br>Even if we would change the semantics of std::reference_wrapper I
<br>would even more strongly disagree with this approach, because it
<br>enforces the usage of the std::swap template even though the
<br>underlying type might not be MoveAssignable or MoveConstructible (but
<br>is swappable). It doesn't respect ADL-lookup for T, so it would behave
<br>differently as any other library functions that swaps T objects.
<br>
<br>&gt; That is instead of swapping std::reference_wrapper(s) to swap obje=
cts that
<br>&gt; are referenced. For example
<br>&gt;
<br>&gt; #include &lt;iostream&gt;
<br>&gt; #include &lt;functional&gt;
<br>&gt; template &lt;class T&gt;
<br>&gt; void swap( std::reference_wrapper&lt;T&gt; &amp;a, std::reference_=
wrapper&lt;T&gt; &amp;b )
<br>&gt; {
<br>&gt; &nbsp;std::swap&lt;typename std::reference_wrapper&lt;T&gt;::<wbr>=
type&gt;( a, b );
<br>&gt; }
<br>&gt;
<br>&gt; int main()
<br>&gt; {
<br>&gt; &nbsp; int x =3D 10, y =3D 20;
<br>&gt; &nbsp; auto rx =3D std::ref( x );
<br>&gt; &nbsp; auto ry =3D std::ref( y );
<br>&gt; &nbsp; std::cout &lt;&lt; "x =3D " &lt;&lt; x
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt; ", y =3D " &lt;&lt; y
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", rx =3D " &lt;&lt; rx
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", ry =3D " &lt;&lt; ry &lt;&lt; std=
::endl;
<br>&gt; &nbsp; std::cout &lt;&lt; std::endl;
<br>&gt; &nbsp; swap( rx, ry );
<br>&gt; &nbsp; std::cout &lt;&lt; "x =3D " &lt;&lt; x
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt; ", y =3D " &lt;&lt; y
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", rx =3D " &lt;&lt; rx
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", ry =3D " &lt;&lt; ry &lt;&lt; std=
::endl;
<br>&gt; &nbsp; std::cout &lt;&lt; std::endl;
<br>&gt; &nbsp;return 0;
<br>&gt; }
<br>
<br>These example don't convince me: std::reference_wrapper objects are
<br>typically used as an intermediate vehicle for a T reference. Except
<br>for the function call operator overload it doesn't provide any
<br>T-specific functionality (I ignore the conversion functions to T,
<br>because they only mean that you can easily get the referenced T). It
<br>seems IMO better to me to leave the swap semantics of
<br>reference_wrapper as it currently is, because it perfectly matches the
<br>existing copy-semantics, which is pointer-like. This is a well-defined
<br>view of T to me. If you don't like the swap-semantics of this wrapper
<br>type, you should convert it to the underling T reference and swap that
<br>one.
<br>
<br>- Daniel
<br></blockquote><div>&nbsp;</div><div>&nbsp;</div><div>Here is an example =
of the utility of&nbsp;the such swap</div><div>&nbsp;</div><div>#include &l=
t;iostream&gt;<br>#include &lt;functional&gt;<br>#include &lt;algorithm&gt;=
<br>#include &lt;vector&gt;<br>#include &lt;iterator&gt;<br>&nbsp;<br>&nbsp=
;<br>namespace std<br>{<br>void swap( std::reference_wrapper&lt;int&gt; &am=
p;a, std::reference_wrapper&lt;int&gt; &amp;b )<br>{<br>&nbsp;&nbsp;&nbsp; =
swap&lt;int&gt;( a, b );<br>}<br>&nbsp;<br>}<br>&nbsp;<br>&nbsp;<br>int mai=
n()<br>{<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int a[] =3D { 1, 5, 2, 6, 9, 3, 7, 8,=
 0 };<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp; std::vector&lt;std::reference_wrapper&lt;int&gt;&g=
t; v;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( int x : a ) std::cout &lt;&lt; x &=
lt;&lt; ' ';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; std::endl;<br>&nbsp;<br>=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp; std::copy_if( std::begin( a ), std::end( a ), std::back_ins=
erter( v ),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; []( int x ) { return ( x % 2 ); } );<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::=
reverse( v.begin(), v.end() );<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( int x : a=
 ) std::cout &lt;&lt; x &lt;&lt; ' ';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::cout &lt;&lt;=
 std::endl;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return =
0;<br>}&nbsp;</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/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_1124_4016898.1365454625061--

.


Author: Vlad from Moscow <vlad.moscow@mail.ru>
Date: Mon, 8 Apr 2013 13:58:55 -0700 (PDT)
Raw View
------=_Part_1988_10639847.1365454735221
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable


On Tuesday, April 9, 2013 12:57:04 AM UTC+4, Vlad from Moscow wrote:
>
>
> On Tuesday, April 9, 2013 12:15:20 AM UTC+4, Daniel Kr=FCgler wrote:
>>
>> 2013/4/8 Vlad from Moscow <vlad....@mail.ru>:=20
>> > In my opinion there is more sense to define std::swap (or member=20
>> function=20
>> > swap of std::reference_wrapper) such a way that it would swap the=20
>> objects=20
>> > referenced by std::reference_wrapper(s)  instead of=20
>> > std::reference_wrapper(s) themselves.=20
>>
>> Could you please make it a bit clearer why this change would be an=20
>> improvement? Currently I cannot agree with that proposal:=20
>> std::reference_wrapper is both CopyAssignable and CopyConstructible=20
>> and I find the current swap semantics quite natural.=20
>>
>> > For example.=20
>> >=20
>> > template <class T>=20
>> > void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b =
)=20
>> > {=20
>> >  std::swap<typename std::reference_wrapper<T>::type>( a, b );=20
>> > }=20
>>
>> Even if we would change the semantics of std::reference_wrapper I=20
>> would even more strongly disagree with this approach, because it=20
>> enforces the usage of the std::swap template even though the=20
>> underlying type might not be MoveAssignable or MoveConstructible (but=20
>> is swappable). It doesn't respect ADL-lookup for T, so it would behave=
=20
>> differently as any other library functions that swaps T objects.=20
>>
>> > That is instead of swapping std::reference_wrapper(s) to swap objects=
=20
>> that=20
>> > are referenced. For example=20
>> >=20
>> > #include <iostream>=20
>> > #include <functional>=20
>> > template <class T>=20
>> > void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b =
)=20
>> > {=20
>> >  std::swap<typename std::reference_wrapper<T>::type>( a, b );=20
>> > }=20
>> >=20
>> > int main()=20
>> > {=20
>> >   int x =3D 10, y =3D 20;=20
>> >   auto rx =3D std::ref( x );=20
>> >   auto ry =3D std::ref( y );=20
>> >   std::cout << "x =3D " << x=20
>> >          << ", y =3D " << y=20
>> >       << ", rx =3D " << rx=20
>> >       << ", ry =3D " << ry << std::endl;=20
>> >   std::cout << std::endl;=20
>> >   swap( rx, ry );=20
>> >   std::cout << "x =3D " << x=20
>> >          << ", y =3D " << y=20
>> >       << ", rx =3D " << rx=20
>> >       << ", ry =3D " << ry << std::endl;=20
>> >   std::cout << std::endl;=20
>> >  return 0;=20
>> > }=20
>>
>> These example don't convince me: std::reference_wrapper objects are=20
>> typically used as an intermediate vehicle for a T reference. Except=20
>> for the function call operator overload it doesn't provide any=20
>> T-specific functionality (I ignore the conversion functions to T,=20
>> because they only mean that you can easily get the referenced T). It=20
>> seems IMO better to me to leave the swap semantics of=20
>> reference_wrapper as it currently is, because it perfectly matches the=
=20
>> existing copy-semantics, which is pointer-like. This is a well-defined=
=20
>> view of T to me. If you don't like the swap-semantics of this wrapper=20
>> type, you should convert it to the underling T reference and swap that=
=20
>> one.=20
>>
>> - Daniel=20
>>
> =20
> =20
> Here is an example of the utility of the such swap
> =20
> #include <iostream>
> #include <functional>
> #include <algorithm>
> #include <vector>
> #include <iterator>
> =20
> =20
> namespace std
> {
> void swap( std::reference_wrapper<int> &a, std::reference_wrapper<int> &b=
 )
> {
>     swap<int>( a, b );
> }
> =20
> }
> =20
> =20
> int main()
> {
> =20
>                 int a[] =3D { 1, 5, 2, 6, 9, 3, 7, 8, 0 };
>                 std::vector<std::reference_wrapper<int>> v;
> =20
>                 for ( int x : a ) std::cout << x << ' ';
>                 std::cout << std::endl;
> =20
>                 std::copy_if( std::begin( a ), std::end( a ),=20
> std::back_inserter( v ),
>                         []( int x ) { return ( x % 2 ); } );
> =20
>                 std::reverse( v.begin(), v.end() );
> =20
>                 for ( int x : a ) std::cout << x << ' ';
>                 std::cout << std::endl;
> =20
>         return 0;
> }=20
> =20
>
=20
The example can be copied and pasted in www.ideone.com=20

--=20

---=20
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 e=
mail 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-proposa=
ls/?hl=3Den.



------=_Part_1988_10639847.1365454735221
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br>On Tuesday, April 9, 2013 12:57:04 AM UTC+4, Vlad from Moscow wrote:<bl=
ockquote style=3D"margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left=
-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: soli=
d;" class=3D"gmail_quote"><br>On Tuesday, April 9, 2013 12:15:20 AM UTC+4, =
Daniel Kr=FCgler wrote:<blockquote style=3D"margin: 0px 0px 0px 0.8ex; padd=
ing-left: 1ex; border-left-color: rgb(204, 204, 204); border-left-width: 1p=
x; border-left-style: solid;" class=3D"gmail_quote">2013/4/8 Vlad from Mosc=
ow &lt;<a>vlad....@mail.ru</a>&gt;:
<br>&gt; In my opinion there is more sense to define std::swap (or member f=
unction
<br>&gt; swap of std::reference_wrapper) such a way that it would swap the =
objects
<br>&gt; referenced by std::reference_wrapper(s) &nbsp;instead of
<br>&gt; std::reference_wrapper(s) themselves.
<br>
<br>Could you please make it a bit clearer why this change would be an
<br>improvement? Currently I cannot agree with that proposal:
<br>std::reference_wrapper is both CopyAssignable and CopyConstructible
<br>and I find the current swap semantics quite natural.
<br>
<br>&gt; For example.
<br>&gt;
<br>&gt; template &lt;class T&gt;
<br>&gt; void swap( std::reference_wrapper&lt;T&gt; &amp;a, std::reference_=
wrapper&lt;T&gt; &amp;b )
<br>&gt; {
<br>&gt; &nbsp;std::swap&lt;typename std::reference_wrapper&lt;T&gt;::<wbr>=
type&gt;( a, b );
<br>&gt; }
<br>
<br>Even if we would change the semantics of std::reference_wrapper I
<br>would even more strongly disagree with this approach, because it
<br>enforces the usage of the std::swap template even though the
<br>underlying type might not be MoveAssignable or MoveConstructible (but
<br>is swappable). It doesn't respect ADL-lookup for T, so it would behave
<br>differently as any other library functions that swaps T objects.
<br>
<br>&gt; That is instead of swapping std::reference_wrapper(s) to swap obje=
cts that
<br>&gt; are referenced. For example
<br>&gt;
<br>&gt; #include &lt;iostream&gt;
<br>&gt; #include &lt;functional&gt;
<br>&gt; template &lt;class T&gt;
<br>&gt; void swap( std::reference_wrapper&lt;T&gt; &amp;a, std::reference_=
wrapper&lt;T&gt; &amp;b )
<br>&gt; {
<br>&gt; &nbsp;std::swap&lt;typename std::reference_wrapper&lt;T&gt;::<wbr>=
type&gt;( a, b );
<br>&gt; }
<br>&gt;
<br>&gt; int main()
<br>&gt; {
<br>&gt; &nbsp; int x =3D 10, y =3D 20;
<br>&gt; &nbsp; auto rx =3D std::ref( x );
<br>&gt; &nbsp; auto ry =3D std::ref( y );
<br>&gt; &nbsp; std::cout &lt;&lt; "x =3D " &lt;&lt; x
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt; ", y =3D " &lt;&lt; y
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", rx =3D " &lt;&lt; rx
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", ry =3D " &lt;&lt; ry &lt;&lt; std=
::endl;
<br>&gt; &nbsp; std::cout &lt;&lt; std::endl;
<br>&gt; &nbsp; swap( rx, ry );
<br>&gt; &nbsp; std::cout &lt;&lt; "x =3D " &lt;&lt; x
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt; ", y =3D " &lt;&lt; y
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", rx =3D " &lt;&lt; rx
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", ry =3D " &lt;&lt; ry &lt;&lt; std=
::endl;
<br>&gt; &nbsp; std::cout &lt;&lt; std::endl;
<br>&gt; &nbsp;return 0;
<br>&gt; }
<br>
<br>These example don't convince me: std::reference_wrapper objects are
<br>typically used as an intermediate vehicle for a T reference. Except
<br>for the function call operator overload it doesn't provide any
<br>T-specific functionality (I ignore the conversion functions to T,
<br>because they only mean that you can easily get the referenced T). It
<br>seems IMO better to me to leave the swap semantics of
<br>reference_wrapper as it currently is, because it perfectly matches the
<br>existing copy-semantics, which is pointer-like. This is a well-defined
<br>view of T to me. If you don't like the swap-semantics of this wrapper
<br>type, you should convert it to the underling T reference and swap that
<br>one.
<br>
<br>- Daniel
<br></blockquote><div>&nbsp;</div><div>&nbsp;</div><div>Here is an example =
of the utility of&nbsp;the such swap</div><div>&nbsp;</div><div>#include &l=
t;iostream&gt;<br>#include &lt;functional&gt;<br>#include &lt;algorithm&gt;=
<br>#include &lt;vector&gt;<br>#include &lt;iterator&gt;<br>&nbsp;<br>&nbsp=
;<br>namespace std<br>{<br>void swap( std::reference_wrapper&lt;int&gt; &am=
p;a, std::reference_wrapper&lt;int&gt; &amp;b )<br>{<br>&nbsp;&nbsp;&nbsp; =
swap&lt;int&gt;( a, b );<br>}<br>&nbsp;<br>}<br>&nbsp;<br>&nbsp;<br>int mai=
n()<br>{<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int a[] =3D { 1, 5, 2, 6, 9, 3, 7, 8,=
 0 };<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp; std::vector&lt;std::reference_<wbr>wrapper&lt;int&=
gt;&gt; v;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( int x : a ) std::cout &lt;&lt=
; x &lt;&lt; ' ';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; std::endl;<br>&nbsp=
;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp; std::copy_if( std::begin( a ), std::end( a ), std::bac=
k_inserter( v ),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp; []( int x ) { return ( x % 2 ); } );<br>&nbsp;<br>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
std::reverse( v.begin(), v.end() );<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( int =
x : a ) std::cout &lt;&lt; x &lt;&lt; ' ';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::cout &lt=
;&lt; std::endl;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; re=
turn 0;<br>}&nbsp;</div><div>&nbsp;</div></blockquote><div>&nbsp;</div><div=
>The example can be copied and pasted in <a href=3D"http://www.ideone.com">=
www.ideone.com</a>&nbsp;</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/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_1988_10639847.1365454735221--

.


Author: Vlad from Moscow <vlad.moscow@mail.ru>
Date: Mon, 8 Apr 2013 14:01:01 -0700 (PDT)
Raw View
------=_Part_455_24173138.1365454861260
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable


On Tuesday, April 9, 2013 12:58:55 AM UTC+4, Vlad from Moscow wrote:
>
>
> On Tuesday, April 9, 2013 12:57:04 AM UTC+4, Vlad from Moscow wrote:
>>
>>
>> On Tuesday, April 9, 2013 12:15:20 AM UTC+4, Daniel Kr=FCgler wrote:
>>>
>>> 2013/4/8 Vlad from Moscow <vlad....@mail.ru>:=20
>>> > In my opinion there is more sense to define std::swap (or member=20
>>> function=20
>>> > swap of std::reference_wrapper) such a way that it would swap the=20
>>> objects=20
>>> > referenced by std::reference_wrapper(s)  instead of=20
>>> > std::reference_wrapper(s) themselves.=20
>>>
>>> Could you please make it a bit clearer why this change would be an=20
>>> improvement? Currently I cannot agree with that proposal:=20
>>> std::reference_wrapper is both CopyAssignable and CopyConstructible=20
>>> and I find the current swap semantics quite natural.=20
>>>
>>> > For example.=20
>>> >=20
>>> > template <class T>=20
>>> > void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b=
=20
>>> )=20
>>> > {=20
>>> >  std::swap<typename std::reference_wrapper<T>::type>( a, b );=20
>>> > }=20
>>>
>>> Even if we would change the semantics of std::reference_wrapper I=20
>>> would even more strongly disagree with this approach, because it=20
>>> enforces the usage of the std::swap template even though the=20
>>> underlying type might not be MoveAssignable or MoveConstructible (but=
=20
>>> is swappable). It doesn't respect ADL-lookup for T, so it would behave=
=20
>>> differently as any other library functions that swaps T objects.=20
>>>
>>> > That is instead of swapping std::reference_wrapper(s) to swap objects=
=20
>>> that=20
>>> > are referenced. For example=20
>>> >=20
>>> > #include <iostream>=20
>>> > #include <functional>=20
>>> > template <class T>=20
>>> > void swap( std::reference_wrapper<T> &a, std::reference_wrapper<T> &b=
=20
>>> )=20
>>> > {=20
>>> >  std::swap<typename std::reference_wrapper<T>::type>( a, b );=20
>>> > }=20
>>> >=20
>>> > int main()=20
>>> > {=20
>>> >   int x =3D 10, y =3D 20;=20
>>> >   auto rx =3D std::ref( x );=20
>>> >   auto ry =3D std::ref( y );=20
>>> >   std::cout << "x =3D " << x=20
>>> >          << ", y =3D " << y=20
>>> >       << ", rx =3D " << rx=20
>>> >       << ", ry =3D " << ry << std::endl;=20
>>> >   std::cout << std::endl;=20
>>> >   swap( rx, ry );=20
>>> >   std::cout << "x =3D " << x=20
>>> >          << ", y =3D " << y=20
>>> >       << ", rx =3D " << rx=20
>>> >       << ", ry =3D " << ry << std::endl;=20
>>> >   std::cout << std::endl;=20
>>> >  return 0;=20
>>> > }=20
>>>
>>> These example don't convince me: std::reference_wrapper objects are=20
>>> typically used as an intermediate vehicle for a T reference. Except=20
>>> for the function call operator overload it doesn't provide any=20
>>> T-specific functionality (I ignore the conversion functions to T,=20
>>> because they only mean that you can easily get the referenced T). It=20
>>> seems IMO better to me to leave the swap semantics of=20
>>> reference_wrapper as it currently is, because it perfectly matches the=
=20
>>> existing copy-semantics, which is pointer-like. This is a well-defined=
=20
>>> view of T to me. If you don't like the swap-semantics of this wrapper=
=20
>>> type, you should convert it to the underling T reference and swap that=
=20
>>> one.=20
>>>
>>> - Daniel=20
>>>
>> =20
>> =20
>> Here is an example of the utility of the such swap
>> =20
>> #include <iostream>
>> #include <functional>
>> #include <algorithm>
>> #include <vector>
>> #include <iterator>
>> =20
>> =20
>> namespace std
>> {
>> void swap( std::reference_wrapper<int> &a, std::reference_wrapper<int> &=
b=20
>> )
>> {
>>     swap<int>( a, b );
>> }
>> =20
>> }
>> =20
>> =20
>> int main()
>> {
>> =20
>>                 int a[] =3D { 1, 5, 2, 6, 9, 3, 7, 8, 0 };
>>                 std::vector<std::reference_wrapper<int>> v;
>> =20
>>                 for ( int x : a ) std::cout << x << ' ';
>>                 std::cout << std::endl;
>> =20
>>                 std::copy_if( std::begin( a ), std::end( a ),=20
>> std::back_inserter( v ),
>>                         []( int x ) { return ( x % 2 ); } );
>> =20
>>                 std::reverse( v.begin(), v.end() );
>> =20
>>                 for ( int x : a ) std::cout << x << ' ';
>>                 std::cout << std::endl;
>> =20
>>         return 0;
>> }=20
>> =20
>>
> =20
> The example can be copied and pasted in www.ideone.com=20
> =20
>
=20
It is the same symantic if we would have
=20
int x =3D 10, y =3D 20;
int &rx =3D x;
int &ry =3D y;
=20
std:;swap( rx, ry );
=20
Why does std::reference_wrapper behave differently?=20

--=20

---=20
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 e=
mail 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-proposa=
ls/?hl=3Den.



------=_Part_455_24173138.1365454861260
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br>On Tuesday, April 9, 2013 12:58:55 AM UTC+4, Vlad from Moscow wrote:<bl=
ockquote style=3D"margin: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left=
-color: rgb(204, 204, 204); border-left-width: 1px; border-left-style: soli=
d;" class=3D"gmail_quote"><br>On Tuesday, April 9, 2013 12:57:04 AM UTC+4, =
Vlad from Moscow wrote:<blockquote style=3D"margin: 0px 0px 0px 0.8ex; padd=
ing-left: 1ex; border-left-color: rgb(204, 204, 204); border-left-width: 1p=
x; border-left-style: solid;" class=3D"gmail_quote"><br>On Tuesday, April 9=
, 2013 12:15:20 AM UTC+4, Daniel Kr=FCgler wrote:<blockquote style=3D"margi=
n: 0px 0px 0px 0.8ex; padding-left: 1ex; border-left-color: rgb(204, 204, 2=
04); border-left-width: 1px; border-left-style: solid;" class=3D"gmail_quot=
e">2013/4/8 Vlad from Moscow &lt;<a>vlad....@mail.ru</a>&gt;:
<br>&gt; In my opinion there is more sense to define std::swap (or member f=
unction
<br>&gt; swap of std::reference_wrapper) such a way that it would swap the =
objects
<br>&gt; referenced by std::reference_wrapper(s) &nbsp;instead of
<br>&gt; std::reference_wrapper(s) themselves.
<br>
<br>Could you please make it a bit clearer why this change would be an
<br>improvement? Currently I cannot agree with that proposal:
<br>std::reference_wrapper is both CopyAssignable and CopyConstructible
<br>and I find the current swap semantics quite natural.
<br>
<br>&gt; For example.
<br>&gt;
<br>&gt; template &lt;class T&gt;
<br>&gt; void swap( std::reference_wrapper&lt;T&gt; &amp;a, std::reference_=
wrapper&lt;T&gt; &amp;b )
<br>&gt; {
<br>&gt; &nbsp;std::swap&lt;typename std::reference_wrapper&lt;T&gt;::<wbr>=
type&gt;( a, b );
<br>&gt; }
<br>
<br>Even if we would change the semantics of std::reference_wrapper I
<br>would even more strongly disagree with this approach, because it
<br>enforces the usage of the std::swap template even though the
<br>underlying type might not be MoveAssignable or MoveConstructible (but
<br>is swappable). It doesn't respect ADL-lookup for T, so it would behave
<br>differently as any other library functions that swaps T objects.
<br>
<br>&gt; That is instead of swapping std::reference_wrapper(s) to swap obje=
cts that
<br>&gt; are referenced. For example
<br>&gt;
<br>&gt; #include &lt;iostream&gt;
<br>&gt; #include &lt;functional&gt;
<br>&gt; template &lt;class T&gt;
<br>&gt; void swap( std::reference_wrapper&lt;T&gt; &amp;a, std::reference_=
wrapper&lt;T&gt; &amp;b )
<br>&gt; {
<br>&gt; &nbsp;std::swap&lt;typename std::reference_wrapper&lt;T&gt;::<wbr>=
type&gt;( a, b );
<br>&gt; }
<br>&gt;
<br>&gt; int main()
<br>&gt; {
<br>&gt; &nbsp; int x =3D 10, y =3D 20;
<br>&gt; &nbsp; auto rx =3D std::ref( x );
<br>&gt; &nbsp; auto ry =3D std::ref( y );
<br>&gt; &nbsp; std::cout &lt;&lt; "x =3D " &lt;&lt; x
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt; ", y =3D " &lt;&lt; y
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", rx =3D " &lt;&lt; rx
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", ry =3D " &lt;&lt; ry &lt;&lt; std=
::endl;
<br>&gt; &nbsp; std::cout &lt;&lt; std::endl;
<br>&gt; &nbsp; swap( rx, ry );
<br>&gt; &nbsp; std::cout &lt;&lt; "x =3D " &lt;&lt; x
<br>&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;&lt; ", y =3D " &lt;&lt; y
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", rx =3D " &lt;&lt; rx
<br>&gt; &nbsp; &nbsp; &nbsp; &lt;&lt; ", ry =3D " &lt;&lt; ry &lt;&lt; std=
::endl;
<br>&gt; &nbsp; std::cout &lt;&lt; std::endl;
<br>&gt; &nbsp;return 0;
<br>&gt; }
<br>
<br>These example don't convince me: std::reference_wrapper objects are
<br>typically used as an intermediate vehicle for a T reference. Except
<br>for the function call operator overload it doesn't provide any
<br>T-specific functionality (I ignore the conversion functions to T,
<br>because they only mean that you can easily get the referenced T). It
<br>seems IMO better to me to leave the swap semantics of
<br>reference_wrapper as it currently is, because it perfectly matches the
<br>existing copy-semantics, which is pointer-like. This is a well-defined
<br>view of T to me. If you don't like the swap-semantics of this wrapper
<br>type, you should convert it to the underling T reference and swap that
<br>one.
<br>
<br>- Daniel
<br></blockquote><div>&nbsp;</div><div>&nbsp;</div><div>Here is an example =
of the utility of&nbsp;the such swap</div><div>&nbsp;</div><div>#include &l=
t;iostream&gt;<br>#include &lt;functional&gt;<br>#include &lt;algorithm&gt;=
<br>#include &lt;vector&gt;<br>#include &lt;iterator&gt;<br>&nbsp;<br>&nbsp=
;<br>namespace std<br>{<br>void swap( std::reference_wrapper&lt;int&gt; &am=
p;a, std::reference_wrapper&lt;int&gt; &amp;b )<br>{<br>&nbsp;&nbsp;&nbsp; =
swap&lt;int&gt;( a, b );<br>}<br>&nbsp;<br>}<br>&nbsp;<br>&nbsp;<br>int mai=
n()<br>{<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int a[] =3D { 1, 5, 2, 6, 9, 3, 7, 8,=
 0 };<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp; std::vector&lt;std::reference_<wbr>wrapper&lt;int&=
gt;&gt; v;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( int x : a ) std::cout &lt;&lt=
; x &lt;&lt; ' ';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; std::endl;<br>&nbsp=
;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp; std::copy_if( std::begin( a ), std::end( a ), std::bac=
k_inserter( v ),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp; []( int x ) { return ( x % 2 ); } );<br>&nbsp;<br>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
std::reverse( v.begin(), v.end() );<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( int =
x : a ) std::cout &lt;&lt; x &lt;&lt; ' ';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::cout &lt=
;&lt; std::endl;<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; re=
turn 0;<br>}&nbsp;</div><div>&nbsp;</div></blockquote><div>&nbsp;</div><div=
>The example can be copied and pasted in <a href=3D"http://www.ideone.com" =
target=3D"_blank">www.ideone.com</a>&nbsp;</div><div>&nbsp;</div></blockquo=
te><div>&nbsp;</div><div>It is the same symantic if we would have</div><div=
>&nbsp;</div><div>int x =3D 10, y =3D 20;</div><div>int &amp;rx =3D x;</div=
><div>int &amp;ry =3D y;</div><div>&nbsp;</div><div>std:;swap( rx, ry );</d=
iv><div>&nbsp;</div><div>Why does std::reference_wrapper behave differently=
?&nbsp;</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/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_455_24173138.1365454861260--

.


Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@gmail.com>
Date: Tue, 9 Apr 2013 00:00:19 +0200
Raw View
2013/4/8 Vlad from Moscow <vlad.moscow@mail.ru>:
>
> It is the same symantic if we would have
>
> int x = 10, y = 20;
> int &rx = x;
> int &ry = y;
>
> std:;swap( rx, ry );

Sure, but swap is not called this way by any algorithm of the standard
library, instead it is called to take advantage of ADL (For int this
doesn't make any difference, so this type is not a good example).
Further-more, std::reference_wrapper *is* not a T object, for example
you cannot invoke any member function of T on it (except the function
call operator). It is a pointer-like wrapper of references.

> Why does std::reference_wrapper behave differently?

Because it's partially pointer-like (but is never empty), it's not
exactly a T value.

The fact that std::reference_wrapper's copy semantics is completely
different to it's swap semantics, looks inconsistent to me. I don't
see why it is necessary to distort std::reference_wrapper for this,
just to realize the example you have provided. I agree that your
example is an interesting one, but there exists a lot of different
ways to realize it, among them a transforming iterator whose operator*
transforms any reference_wrapper<T> value as T&. This would be similar
to the common idiom that reference_wrapper objects are unpacked at
dedicated points.

std::reference_wrapper is intended to be a light-weight wrapper for
some referenced type, IMO we should not change this by introducing a
deep swap semantics.

- Daniel

--

---
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/?hl=en.



.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Mon, 8 Apr 2013 20:35:27 -0700 (PDT)
Raw View
------=_Part_67_11899442.1365478527940
Content-Type: text/plain; charset=ISO-8859-1



On Monday, April 8, 2013 12:57:08 PM UTC-7, Vlad from Moscow wrote:
>
> In my opinion there is more sense to define std::swap (or member function
> swap of std::reference_wrapper) such a way that it would swap the objects
> referenced by std::reference_wrapper(s)  instead of
> std::reference_wrapper(s) themselves.
>

OK, let's completely ignore the obvious and unacceptable backwards
compatibility problems this creates, thus making it untenable. I'll just
ask a simple question:

What if I *want* to swap the reference wrappers? With your way, swapping
them would be *impossible* idiomatically; they'd have to add some
`swap_wrapper` function to the class, which makes swapping the wrapper
objects themselves non-idiomatic.

reference_wrapper is intended to be a way to make references to objects
that behave kind of like pointers, so that code that uses template
parameters that are expected to be pointer-like will work. That means that
swapping the wrapper object swaps the wrapper, not what's being pointed to.
Just like for pointers.

--

---
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/?hl=en.



------=_Part_67_11899442.1365478527940
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br><br>On Monday, April 8, 2013 12:57:08 PM UTC-7, Vlad from Moscow wrote:=
<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;bor=
der-left: 1px #ccc solid;padding-left: 1ex;"><div>In my opinion there is mo=
re sense to define std::swap (or member function swap of std::reference_wra=
pper) such a way that it would swap the objects referenced by std::referenc=
e_wrapper(s)&nbsp; instead of std::reference_wrapper(s) themselves.</div></=
blockquote><div><br>OK, let's completely ignore the obvious and unacceptabl=
e backwards compatibility problems this creates, thus making it untenable. =
I'll just ask a simple question:<br><br>What if I <i>want</i> to swap the r=
eference wrappers? With your way, swapping them would be <i>impossible</i> =
idiomatically; they'd have to add some `swap_wrapper` function to the class=
, which makes swapping the wrapper objects themselves non-idiomatic. <br><b=
r>reference_wrapper is intended to be a way to make references to objects t=
hat behave kind of like pointers, so that code that uses template parameter=
s that are expected to be pointer-like will work. That means that swapping =
the wrapper object swaps the wrapper, not what's being pointed to. Just lik=
e for pointers.<br></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/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_67_11899442.1365478527940--

.