Topic: rvalue to lvalue conversion function


Author: Francisco <francisco.mailing.lists@oblita.com>
Date: Wed, 19 Nov 2014 17:07:15 -0800 (PST)
Raw View
------=_Part_6069_1137618689.1416445635098
Content-Type: multipart/alternative;
 boundary="----=_Part_6070_910396782.1416445635099"

------=_Part_6070_910396782.1416445635099
Content-Type: text/plain; charset=UTF-8



Recently I've discovered <http://stackoverflow.com/q/26793072/1000282> I can turn rvalues into lvalues through this template

template <typename T>inline constexpr typename std::remove_reference<T>::type &make_ref(T &&r) noexcept {
    using U = typename std::remove_reference<T>::type;
    return static_cast<U &>(r);}

This is useful for me in the following example situation:

string get_my_file_contents() {
    return {std::istreambuf_iterator<char>(make_ref(std::ifstream("my_file.txt"))),
            std::istreambuf_iterator<char>()};
}

I mean, it's useful in any case where you have a function that accepts an lvalue
reference as parameter solely, but you don't want to create a temporary variable
in the scope just to pass it to a function call, so you consciously make a lvalue
out of a rvalue inside the expression.

Without this, I would have to do:

string get_my_file_contents() {
    std::ifstream temp("my_file.txt");
    return {std::istreambuf_iterator<char>(temp),
            std::istreambuf_iterator<char>()};
};

There's some wording <http://stackoverflow.com/a/26793404/1000282> added to C++14 standard about reinterpret_cast
that relates to this kind of conversion I believe, for which reasons
of inclusion I have no idea for sure.

This also make it possible to temporarily get addresses of rvalues (just to pass as
argument for example) and what not.

--

---
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/.

------=_Part_6070_910396782.1416445635099
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><pre style=3D"" class=3D"lang-cpp prettyprint prettyprinte=
d"><code><span class=3D"kwd"><code><span class=3D"str"></span><a href=3D"ht=
tp://stackoverflow.com/q/26793072/1000282">Recently I've discovered</a> I c=
an turn rvalues into lvalues through this template</code><br><br>template</=
span><span class=3D"pln"> </span><span class=3D"pun">&lt;</span><span class=
=3D"kwd">typename</span><span class=3D"pln"> T</span><span class=3D"pun">&g=
t;</span><span class=3D"pln">
</span><span class=3D"kwd">inline</span><span class=3D"pln"> </span><span c=
lass=3D"kwd">constexpr</span><span class=3D"pln"> </span><span class=3D"kwd=
">typename</span><span class=3D"pln"> std</span><span class=3D"pun">::</spa=
n><span class=3D"pln">remove_reference</span><span class=3D"pun">&lt;</span=
><span class=3D"pln">T</span><span class=3D"pun">&gt;::</span><span class=
=3D"pln">type </span><span class=3D"pun">&amp;</span><span class=3D"pln">ma=
ke_ref</span><span class=3D"pun">(</span><span class=3D"pln">T </span><span=
 class=3D"pun">&amp;&amp;</span><span class=3D"pln">r</span><span class=3D"=
pun">)</span><span class=3D"pln"> noexcept </span><span class=3D"pun">{</sp=
an><span class=3D"pln">
    </span><span class=3D"kwd">using</span><span class=3D"pln"> U </span><s=
pan class=3D"pun">=3D</span><span class=3D"pln"> </span><span class=3D"kwd"=
>typename</span><span class=3D"pln"> std</span><span class=3D"pun">::</span=
><span class=3D"pln">remove_reference</span><span class=3D"pun">&lt;</span>=
<span class=3D"pln">T</span><span class=3D"pun">&gt;::</span><span class=3D=
"pln">type</span><span class=3D"pun">;</span><span class=3D"pln">
    </span><span class=3D"kwd">return</span><span class=3D"pln"> </span><sp=
an class=3D"kwd">static_cast</span><span class=3D"pun">&lt;</span><span cla=
ss=3D"pln">U </span><span class=3D"pun">&amp;&gt;(</span><span class=3D"pln=
">r</span><span class=3D"pun">);</span><span class=3D"pln">
</span><span class=3D"pun">}<br><br>This is useful for me in the following =
example situation:<br><br>string get_my_file_contents() {<br>    return {st=
d::istreambuf_iterator&lt;char&gt;(make_ref(std::ifstream("my_file.txt"))),=
<br>            std::istreambuf_iterator&lt;char&gt;()};<br>}<br><br>I mean=
, it's useful in any case where you have a function that accepts an lvalue<=
br>reference as parameter solely, but you don't want to create a temporary =
variable<br>in the scope just to pass it to a function call, so you conscio=
usly make a lvalue<br>out of a rvalue inside the expression.<br><br>Without=
 this, I would have to do</span>:</code><br><br>string get_my_file_contents=
() {<br>    std::ifstream temp("my_file.txt");<br>    return {std::istreamb=
uf_iterator&lt;char&gt;(temp),<br>            std::istreambuf_iterator&lt;c=
har&gt;()};<br>};<br><br><a href=3D"http://stackoverflow.com/a/26793404/100=
0282">There's some wording</a> added to C++14 standard about reinterpret_ca=
st<br>that relates to this kind of conversion I believe, for which reasons<=
br>of inclusion I have no idea for sure.<br><br>This also make it possible =
to temporarily get addresses of rvalues (just to pass as<br>argument for ex=
ample) and what not.<br></pre></div>

<p></p>

-- <br />
<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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<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 />

------=_Part_6070_910396782.1416445635099--
------=_Part_6069_1137618689.1416445635098--

.


Author: David Krauss <potswa@gmail.com>
Date: Sat, 22 Nov 2014 07:03:21 +0800
Raw View
--Apple-Mail=_6F991C30-3EC7-496B-B064-441F4677DE41
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=UTF-8


On 2014=E2=80=9311=E2=80=9320, at 9:07 AM, Francisco <francisco.mailing.lis=
ts@oblita.com> wrote:

> Recently I've discovered I can turn rvalues into lvalues through this tem=
plate
>=20
> template <typename T>
> inline constexpr typename std::remove_reference<T>::type &make_ref(T &&r)=
 noexcept {
>     using U =3D typename std::remove_reference<T>::type;
>     return static_cast<U &>(r);
> }
This helps to defeat the safety built into rvalue-specific interfaces. Crea=
ting such a convenience facility is your own business, but I can=E2=80=99t =
imagine a proposal gaining traction.
> There's some wording added to C++14 standard about reinterpret_cast
> that relates to this kind of conversion I believe, for which reasons
> of inclusion I have no idea for sure.
Yes, and it was apparently controversial.
> This also make it possible to temporarily get addresses of rvalues (just =
to pass as
> argument for example) and what not.
There=E2=80=99s nothing reinterpret_cast can do here that static_cast toget=
her with const_cast can=E2=80=99t. The static_cast would need to use T cons=
t& , adding const to get the lvalue reference to bind an rvalue, and then t=
he const_cast just removes that const to obtain the original type. Actually=
 writing that for the sake of convenience is madness. CWG was probably (I w=
ould hope) considering some instances of tricky generic code when they (fin=
ally) accepted the change.

--=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/.

--Apple-Mail=_6F991C30-3EC7-496B-B064-441F4677DE41
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=UTF-8

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>On 2014=
=E2=80=9311=E2=80=9320, at 9:07 AM, Francisco &lt;<a href=3D"mailto:francis=
co.mailing.lists@oblita.com">francisco.mailing.lists@oblita.com</a>&gt; wro=
te:</div><br class=3D"Apple-interchange-newline"><blockquote type=3D"cite">=
<div dir=3D"ltr"><pre style=3D"" class=3D"lang-cpp prettyprint prettyprinte=
d"><code><span class=3D"kwd"><code><span class=3D"str"></span><a href=3D"ht=
tp://stackoverflow.com/q/26793072/1000282">Recently I've discovered</a> I c=
an turn rvalues into lvalues through this template</code><br><br>template</=
span><span class=3D"pln"> </span><span class=3D"pun">&lt;</span><span class=
=3D"kwd">typename</span><span class=3D"pln"> T</span><span class=3D"pun">&g=
t;</span><span class=3D"pln">
</span><span class=3D"kwd">inline</span><span class=3D"pln"> </span><span c=
lass=3D"kwd">constexpr</span><span class=3D"pln"> </span><span class=3D"kwd=
">typename</span><span class=3D"pln"> std</span><span class=3D"pun">::</spa=
n><span class=3D"pln">remove_reference</span><span class=3D"pun">&lt;</span=
><span class=3D"pln">T</span><span class=3D"pun">&gt;::</span><span class=
=3D"pln">type </span><span class=3D"pun">&amp;</span><span class=3D"pln">ma=
ke_ref</span><span class=3D"pun">(</span><span class=3D"pln">T </span><span=
 class=3D"pun">&amp;&amp;</span><span class=3D"pln">r</span><span class=3D"=
pun">)</span><span class=3D"pln"> noexcept </span><span class=3D"pun">{</sp=
an><span class=3D"pln">
    </span><span class=3D"kwd">using</span><span class=3D"pln"> U </span><s=
pan class=3D"pun">=3D</span><span class=3D"pln"> </span><span class=3D"kwd"=
>typename</span><span class=3D"pln"> std</span><span class=3D"pun">::</span=
><span class=3D"pln">remove_reference</span><span class=3D"pun">&lt;</span>=
<span class=3D"pln">T</span><span class=3D"pun">&gt;::</span><span class=3D=
"pln">type</span><span class=3D"pun">;</span><span class=3D"pln">
    </span><span class=3D"kwd">return</span><span class=3D"pln"> </span><sp=
an class=3D"kwd">static_cast</span><span class=3D"pun">&lt;</span><span cla=
ss=3D"pln">U </span><span class=3D"pun">&amp;&gt;(</span><span class=3D"pln=
">r</span><span class=3D"pun">);</span><span class=3D"pln">
</span><span class=3D"pun">}<br></span></code></pre></div></blockquote><div=
>This helps to defeat the safety built into rvalue-specific interfaces. Cre=
ating such a convenience facility is your own business, but I can=E2=80=99t=
 imagine a proposal gaining traction.</div><blockquote type=3D"cite"><div d=
ir=3D"ltr"><pre style=3D"" class=3D"lang-cpp prettyprint prettyprinted"><a =
href=3D"http://stackoverflow.com/a/26793404/1000282">There's some wording</=
a> added to C++14 standard about reinterpret_cast<br>that relates to this k=
ind of conversion I believe, for which reasons<br>of inclusion I have no id=
ea for sure.<br></pre></div></blockquote><div>Yes, and it was apparently co=
ntroversial.</div><blockquote type=3D"cite"><div dir=3D"ltr"><pre style=3D"=
" class=3D"lang-cpp prettyprint prettyprinted">This also make it possible t=
o temporarily get addresses of rvalues (just to pass as<br>argument for exa=
mple) and what not.<br></pre></div></blockquote>There=E2=80=99s nothing <fo=
nt face=3D"Courier">reinterpret_cast</font> can do here that <font face=3D"=
Courier">static_cast</font> together with&nbsp;<font face=3D"Courier">const=
_cast</font> can=E2=80=99t. The <font face=3D"Courier">static_cast</font> w=
ould need to use <font face=3D"Courier">T const&amp;</font>&nbsp;, adding <=
font face=3D"Courier">const</font> to get the lvalue reference to bind an r=
value, and then the <font face=3D"Courier">const_cast</font> just removes t=
hat&nbsp;<font face=3D"Courier">const</font> to obtain the original type. A=
ctually writing that for the sake of convenience is madness. CWG was probab=
ly (I would hope) considering some instances of tricky generic code when th=
ey (finally) accepted the change.</div></body></html>

<p></p>

-- <br />
<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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<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 />

--Apple-Mail=_6F991C30-3EC7-496B-B064-441F4677DE41--

.


Author: oblita@gmail.com
Date: Sat, 22 Nov 2014 07:09:20 -0800 (PST)
Raw View
------=_Part_1413_1450422921.1416668960145
Content-Type: multipart/alternative;
 boundary="----=_Part_1414_1171156150.1416668960145"

------=_Part_1414_1171156150.1416668960145
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Thanks for the reply David.

Also, because I thought there was a problem sending the topic since it=20
didn't show up
for more than a day, I ended up creating a duplicate, sorry for that.

To tell the truth, even though it's quite useful for me in several=20
instances, I have lost
interest in it gaining traction to be in the standard library. I found=20
another person <http://stackoverflow.com/a/27053414/1000282>
using the same facility and named it "no_move", which is interesting since=
=20
it does
just the reversal of move.

On Friday, November 21, 2014 9:03:32 PM UTC-2, David Krauss wrote:
>
>
> On 2014=E2=80=9311=E2=80=9320, at 9:07 AM, Francisco <francisco.m...@obli=
ta.com=20
> <javascript:>> wrote:
>
> Recently I've discovered <http://stackoverflow.com/q/26793072/1000282> I =
can turn rvalues into lvalues through this template
>
> template <typename T>inline constexpr typename std::remove_reference<T>::=
type &make_ref(T &&r) noexcept {
>     using U =3D typename std::remove_reference<T>::type;
>     return static_cast<U &>(r);}
>
> This helps to defeat the safety built into rvalue-specific interfaces.=20
> Creating such a convenience facility is your own business, but I can=E2=
=80=99t=20
> imagine a proposal gaining traction.
>
> There's some wording <http://stackoverflow.com/a/26793404/1000282> added =
to C++14 standard about reinterpret_cast
> that relates to this kind of conversion I believe, for which reasons
> of inclusion I have no idea for sure.
>
> Yes, and it was apparently controversial.
>
> This also make it possible to temporarily get addresses of rvalues (just =
to pass as
> argument for example) and what not.
>
> There=E2=80=99s nothing reinterpret_cast can do here that static_cast tog=
ether=20
> with const_cast can=E2=80=99t. The static_cast would need to use T const&=
 ,=20
> adding const to get the lvalue reference to bind an rvalue, and then the=
=20
> const_cast just removes that const to obtain the original type. Actually=
=20
> writing that for the sake of convenience is madness. CWG was probably (I=
=20
> would hope) considering some instances of tricky generic code when they=
=20
> (finally) accepted the change.
>

--=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/.

------=_Part_1414_1171156150.1416668960145
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Thanks for the reply David.<br><br>Also, because I thought=
 there was a problem sending the topic since it didn't show up<br>for more =
than a day, I ended up creating a duplicate, sorry for that.<br><br>To tell=
 the truth, even though it's quite useful for me in several instances, I ha=
ve lost<br>interest in it gaining traction to be in the standard library. <=
a href=3D"http://stackoverflow.com/a/27053414/1000282">I found another pers=
on</a><br>using the same facility and named it "no_move", which is interest=
ing since it does<br>just the reversal of move.<br><br>On Friday, November =
21, 2014 9:03:32 PM UTC-2, David Krauss wrote:<blockquote class=3D"gmail_qu=
ote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padd=
ing-left: 1ex;"><div style=3D"word-wrap:break-word"><br><div><div>On 2014=
=E2=80=9311=E2=80=9320, at 9:07 AM, Francisco &lt;<a href=3D"javascript:" t=
arget=3D"_blank" gdf-obfuscated-mailto=3D"ZtiMfZ35tYAJ" onmousedown=3D"this=
..href=3D'javascript:';return true;" onclick=3D"this.href=3D'javascript:';re=
turn true;">francisco.m...@<wbr>oblita.com</a>&gt; wrote:</div><br><blockqu=
ote type=3D"cite"><div dir=3D"ltr"><pre><code><span><code><span></span><a h=
ref=3D"http://stackoverflow.com/q/26793072/1000282" target=3D"_blank" onmou=
sedown=3D"this.href=3D'http://www.google.com/url?q\75http%3A%2F%2Fstackover=
flow.com%2Fq%2F26793072%2F1000282\46sa\75D\46sntz\0751\46usg\75AFQjCNEnbUx4=
PXDYNg7RpskTPbzJ7CL0oA';return true;" onclick=3D"this.href=3D'http://www.go=
ogle.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fq%2F26793072%2F1000282\46=
sa\75D\46sntz\0751\46usg\75AFQjCNEnbUx4PXDYNg7RpskTPbzJ7CL0oA';return true;=
">Recently I've discovered</a> I can turn rvalues into lvalues through this=
 template</code><br><br>template</span><span> </span><span>&lt;</span><span=
>typename</span><span> T</span><span>&gt;</span><span>
</span><span>inline</span><span> </span><span>constexpr</span><span> </span=
><span>typename</span><span> std</span><span>::</span><span>remove_referenc=
e</span><span>&lt;</span><span>T</span><span>&gt;::</span><span>type </span=
><span>&amp;</span><span>make_ref</span><span>(</span><span>T </span><span>=
&amp;&amp;</span><span>r</span><span>)</span><span> noexcept </span><span>{=
</span><span>
    </span><span>using</span><span> U </span><span>=3D</span><span> </span>=
<span>typename</span><span> std</span><span>::</span><span>remove_reference=
</span><span>&lt;</span><span>T</span><span>&gt;::</span><span>type</span><=
span><wbr>;</span><span>
    </span><span>return</span><span> </span><span>static_cast</span><span>&=
lt;</span><span>U </span><span>&amp;&gt;(</span><span>r</span><span>);</spa=
n><span>
</span><span>}<br></span></code></pre></div></blockquote><div>This helps to=
 defeat the safety built into rvalue-specific interfaces. Creating such a c=
onvenience facility is your own business, but I can=E2=80=99t imagine a pro=
posal gaining traction.</div><blockquote type=3D"cite"><div dir=3D"ltr"><pr=
e><a href=3D"http://stackoverflow.com/a/26793404/1000282" target=3D"_blank"=
 onmousedown=3D"this.href=3D'http://www.google.com/url?q\75http%3A%2F%2Fsta=
ckoverflow.com%2Fa%2F26793404%2F1000282\46sa\75D\46sntz\0751\46usg\75AFQjCN=
F2IY84RqyILVbLBrgabilzpBSa4A';return true;" onclick=3D"this.href=3D'http://=
www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fa%2F26793404%2F1000=
282\46sa\75D\46sntz\0751\46usg\75AFQjCNF2IY84RqyILVbLBrgabilzpBSa4A';return=
 true;">There's some wording</a> added to C++14 standard about reinterpret_=
cast<br>that relates to this kind of conversion I believe, for which reason=
s<br>of inclusion I have no idea for sure.<br></pre></div></blockquote><div=
>Yes, and it was apparently controversial.</div><blockquote type=3D"cite"><=
div dir=3D"ltr"><pre>This also make it possible to temporarily get addresse=
s of rvalues (just to pass as<br>argument for example) and what not.<br></p=
re></div></blockquote>There=E2=80=99s nothing <font face=3D"Courier">reinte=
rpret_cast</font> can do here that <font face=3D"Courier">static_cast</font=
> together with&nbsp;<font face=3D"Courier">const_cast</font> can=E2=80=99t=
.. The <font face=3D"Courier">static_cast</font> would need to use <font fac=
e=3D"Courier">T const&amp;</font>&nbsp;, adding <font face=3D"Courier">cons=
t</font> to get the lvalue reference to bind an rvalue, and then the <font =
face=3D"Courier">const_cast</font> just removes that&nbsp;<font face=3D"Cou=
rier">const</font> to obtain the original type. Actually writing that for t=
he sake of convenience is madness. CWG was probably (I would hope) consider=
ing some instances of tricky generic code when they (finally) accepted the =
change.</div></div></blockquote></div>

<p></p>

-- <br />
<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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<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 />

------=_Part_1414_1171156150.1416668960145--
------=_Part_1413_1450422921.1416668960145--

.