Topic: Opinions on a rvalue to lvalue convertion function


Author: Francisco <francisco.mailing.lists@oblita.com>
Date: Thu, 20 Nov 2014 14:10:39 -0800 (PST)
Raw View
------=_Part_7105_187386828.1416521439781
Content-Type: multipart/alternative;
 boundary="----=_Part_7106_1199391694.1416521439781"

------=_Part_7106_1199391694.1416521439781
Content-Type: text/plain; charset=UTF-8

Recently I've discovered <http://stackoverflow.com/q/26793072/1000282> that
sometimes being able to turn rvalues into
lvalues can be useful for me.

I've been using the following tool:

#include <type_traits>

template <typename T>
inline constexpr std::remove_reference_t<T> &lvalue(T &&r) noexcept {
    return static_cast<std::remove_reference_t<T> &>(r);
}

It's useful when you have to use functions that require lvalues as
arguments, but you don't have any interest in what those particular
values get changed into. For when you are interested in other output
vectors that are not related to the given specific argument.

For example, this:

std::string get_my_file() {
    auto ifs = std::ifstream("myfile.txt");
    return {std::istreambuf_iterator<char>(ifs),
            std::istreambuf_iterator<char>()};
}

can be changed to this:

std::string get_my_file() {
    return
{std::istreambuf_iterator<char>(lvalue(std::ifstream("myfile.txt"))),
            std::istreambuf_iterator<char>()};
}

And this:

std::string temp1 = get_my_shader();
const char *temp2 = temp1.c_str();
glShaderSource(a, 1, &temp2, nullptr);

can be changed to this:

glShaderSource(a, 1, &lvalue(get_my_shader().c_str()), nullptr);

There's a recent change to the standard about reinterpret_cast and xvalues
that seems to be on topic:

http://stackoverflow.com/a/26793404/1000282

Regards,
Francisco Lopes

--

---
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_7106_1199391694.1416521439781
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><a href=3D"http://stackoverflow.com/q/26793072/1000282">Re=
cently I've discovered</a> that sometimes being able to turn rvalues into<b=
r>lvalues can be useful for me.<br><br>I've been using the following tool:<=
br><br><span style=3D"font-family: courier new,monospace;">#include &lt;typ=
e_traits&gt;<br><br>template &lt;typename T&gt;<br>inline constexpr std::re=
move_reference_t&lt;T&gt; &amp;lvalue(T &amp;&amp;r) noexcept {<br>&nbsp;&n=
bsp;&nbsp; return static_cast&lt;std::remove_reference_t&lt;T&gt; &amp;&gt;=
(r);<br>}</span><br><br>It's useful when you have to use functions that req=
uire lvalues as<br>arguments, but you don't have any interest in what those=
 particular<br>values get changed into. For when you are interested in othe=
r output<br>vectors that are not related to the given specific argument.<br=
><br>For example, this:<br><br><span style=3D"font-family: courier new,mono=
space;">std::string get_my_file() {<br>&nbsp;&nbsp;&nbsp; auto ifs =3D std:=
:ifstream("myfile.txt");<br>&nbsp;&nbsp;&nbsp; return {std::istreambuf_iter=
ator&lt;char&gt;(ifs),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp; std::istreambuf_iterator&lt;char&gt;()};<br>}</span><br><=
br>can be changed to this:<br><br><span style=3D"font-family: courier new,m=
onospace;">std::string get_my_file() {<br>&nbsp;&nbsp;&nbsp; return {std::i=
streambuf_iterator&lt;char&gt;(lvalue(std::ifstream("myfile.txt"))),<br>&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::istrea=
mbuf_iterator&lt;char&gt;()};<br>}</span><br><br>And this:<br><br><span sty=
le=3D"font-family: courier new,monospace;">std::string temp1 =3D get_my_sha=
der();<br>const char *temp2 =3D temp1.c_str();<br>glShaderSource(a, 1, &amp=
;temp2, nullptr);</span><br><br>can be changed to this:<br><br><span style=
=3D"font-family: courier new,monospace;">glShaderSource(a, 1, &amp;lvalue(g=
et_my_shader().c_str()), nullptr);</span><br><br>There's a recent change to=
 the standard about reinterpret_cast and xvalues<br>that seems to be on top=
ic:<br><br>http://stackoverflow.com/a/26793404/1000282<br><br>Regards,<br>F=
rancisco Lopes<br></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_7106_1199391694.1416521439781--
------=_Part_7105_187386828.1416521439781--

.


Author: oblita@gmail.com
Date: Sat, 22 Nov 2014 06:27:49 -0800 (PST)
Raw View
------=_Part_1344_120154441.1416666469542
Content-Type: multipart/alternative;
 boundary="----=_Part_1345_801061707.1416666469542"

------=_Part_1345_801061707.1416666469542
Content-Type: text/plain; charset=UTF-8

Simplifying function definition to:

template <typename T>constexpr T &lvalue(T &&r) noexcept { return r; }

Also, I would like to emphasize its sole use is temporary,
internal to the expression it's used.


On Thursday, November 20, 2014 8:10:39 PM UTC-2, Francisco wrote:
>
> Recently I've discovered <http://stackoverflow.com/q/26793072/1000282>
> that sometimes being able to turn rvalues into
> lvalues can be useful for me.
>
> I've been using the following tool:
>
> #include <type_traits>
>
> template <typename T>
> inline constexpr std::remove_reference_t<T> &lvalue(T &&r) noexcept {
>     return static_cast<std::remove_reference_t<T> &>(r);
> }
>
> It's useful when you have to use functions that require lvalues as
> arguments, but you don't have any interest in what those particular
> values get changed into. For when you are interested in other output
> vectors that are not related to the given specific argument.
>
> For example, this:
>
> std::string get_my_file() {
>     auto ifs = std::ifstream("myfile.txt");
>     return {std::istreambuf_iterator<char>(ifs),
>             std::istreambuf_iterator<char>()};
> }
>
> can be changed to this:
>
> std::string get_my_file() {
>     return
> {std::istreambuf_iterator<char>(lvalue(std::ifstream("myfile.txt"))),
>             std::istreambuf_iterator<char>()};
> }
>
> And this:
>
> std::string temp1 = get_my_shader();
> const char *temp2 = temp1.c_str();
> glShaderSource(a, 1, &temp2, nullptr);
>
> can be changed to this:
>
> glShaderSource(a, 1, &lvalue(get_my_shader().c_str()), nullptr);
>
> There's a recent change to the standard about reinterpret_cast and xvalues
> that seems to be on topic:
>
> http://stackoverflow.com/a/26793404/1000282
>
> Regards,
> Francisco Lopes
>

--

---
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_1345_801061707.1416666469542
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Simplifying function definition to:<br><br><pre><span styl=
e=3D"font-family: courier new,monospace;"><span class=3D"n">template</span>=
 <span class=3D"o">&lt;</span><span class=3D"kr">typename</span> <span clas=
s=3D"n">T</span><span class=3D"o">&gt;</span>
<span class=3D"n">constexpr</span> <span class=3D"n">T</span> <span class=
=3D"o">&amp;</span><span class=3D"n">lvalue</span><span class=3D"p">(</span=
><span class=3D"n">T</span> <span class=3D"o">&amp;&amp;</span><span class=
=3D"n">r</span><span class=3D"p">)</span> <span class=3D"n">noexcept</span>=
 <span class=3D"p">{</span> <span class=3D"k">return</span> <span class=3D"=
n">r</span><span class=3D"p">;</span> <span class=3D"p">}<br><br></span></s=
pan>Also, I would like to emphasize its sole use is temporary,<br>internal =
to the expression it's used.<br><br></pre><br>On Thursday, November 20, 201=
4 8:10:39 PM UTC-2, Francisco wrote:<blockquote class=3D"gmail_quote" style=
=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: =
1ex;"><div dir=3D"ltr"><a href=3D"http://stackoverflow.com/q/26793072/10002=
82" target=3D"_blank" onmousedown=3D"this.href=3D'http://www.google.com/url=
?q\75http%3A%2F%2Fstackoverflow.com%2Fq%2F26793072%2F1000282\46sa\75D\46snt=
z\0751\46usg\75AFQjCNEnbUx4PXDYNg7RpskTPbzJ7CL0oA';return true;" onclick=3D=
"this.href=3D'http://www.google.com/url?q\75http%3A%2F%2Fstackoverflow.com%=
2Fq%2F26793072%2F1000282\46sa\75D\46sntz\0751\46usg\75AFQjCNEnbUx4PXDYNg7Rp=
skTPbzJ7CL0oA';return true;">Recently I've discovered</a> that sometimes be=
ing able to turn rvalues into<br>lvalues can be useful for me.<br><br>I've =
been using the following tool:<br><br><span style=3D"font-family:courier ne=
w,monospace">#include &lt;type_traits&gt;<br><br>template &lt;typename T&gt=
;<br>inline constexpr std::remove_reference_t&lt;T&gt; &amp;lvalue(T &amp;&=
amp;r) noexcept {<br>&nbsp;&nbsp;&nbsp; return static_cast&lt;std::remove_<=
wbr>reference_t&lt;T&gt; &amp;&gt;(r);<br>}</span><br><br>It's useful when =
you have to use functions that require lvalues as<br>arguments, but you don=
't have any interest in what those particular<br>values get changed into. F=
or when you are interested in other output<br>vectors that are not related =
to the given specific argument.<br><br>For example, this:<br><br><span styl=
e=3D"font-family:courier new,monospace">std::string get_my_file() {<br>&nbs=
p;&nbsp;&nbsp; auto ifs =3D std::ifstream("myfile.txt");<br>&nbsp;&nbsp;&nb=
sp; return {std::istreambuf_iterator&lt;<wbr>char&gt;(ifs),<br>&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::istreambuf_iter=
ator&lt;char&gt;<wbr>()};<br>}</span><br><br>can be changed to this:<br><br=
><span style=3D"font-family:courier new,monospace">std::string get_my_file(=
) {<br>&nbsp;&nbsp;&nbsp; return {std::istreambuf_iterator&lt;<wbr>char&gt;=
(lvalue(std::ifstream("<wbr>myfile.txt"))),<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; std::istreambuf_iterator&lt;char&gt;=
<wbr>()};<br>}</span><br><br>And this:<br><br><span style=3D"font-family:co=
urier new,monospace">std::string temp1 =3D get_my_shader();<br>const char *=
temp2 =3D temp1.c_str();<br>glShaderSource(a, 1, &amp;temp2, nullptr);</spa=
n><br><br>can be changed to this:<br><br><span style=3D"font-family:courier=
 new,monospace">glShaderSource(a, 1, &amp;lvalue(get_my_shader().c_str(<wbr=
>)), nullptr);</span><br><br>There's a recent change to the standard about =
reinterpret_cast and xvalues<br>that seems to be on topic:<br><br><a href=
=3D"http://stackoverflow.com/a/26793404/1000282" target=3D"_blank" onmoused=
own=3D"this.href=3D'http://www.google.com/url?q\75http%3A%2F%2Fstackoverflo=
w.com%2Fa%2F26793404%2F1000282\46sa\75D\46sntz\0751\46usg\75AFQjCNF2IY84Rqy=
ILVbLBrgabilzpBSa4A';return true;" onclick=3D"this.href=3D'http://www.googl=
e.com/url?q\75http%3A%2F%2Fstackoverflow.com%2Fa%2F26793404%2F1000282\46sa\=
75D\46sntz\0751\46usg\75AFQjCNF2IY84RqyILVbLBrgabilzpBSa4A';return true;">h=
ttp://stackoverflow.com/a/<wbr>26793404/1000282</a><br><br>Regards,<br>Fran=
cisco Lopes<br></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_1345_801061707.1416666469542--
------=_Part_1344_120154441.1416666469542--

.