<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[mit type traits auf rvalue testen?]]></title><description><![CDATA[<p>Hi,</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;
#include &lt;iostream&gt;

template&lt;class T&gt;
void test(T)
{
    std::cout &lt;&lt; &quot;is rvalue: &quot; &lt;&lt; std::is_rvalue_reference&lt;T&gt;::value &lt;&lt; &quot;\n&quot;;
}

int main()
{
    int x = 0;
    test(x);
    test(x + 1);
    return 0;
}
</code></pre>
<p>Gibt bei mir immer &quot;is rvalue: 0&quot; aus.<br />
Geht das generell so nicht (mache ich was falsch),<br />
oder liegt es vielleicht an meinen compiler (g++ 4.5)?</p>
<p>thx <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/294508/mit-type-traits-auf-rvalue-testen</link><generator>RSS for Node</generator><lastBuildDate>Sat, 15 Aug 2026 21:39:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/294508.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 26 Oct 2011 10:45:05 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 10:46:52 GMT]]></title><description><![CDATA[<p>Hi,</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;
#include &lt;iostream&gt;

template&lt;class T&gt;
void test(T)
{
    std::cout &lt;&lt; &quot;is rvalue: &quot; &lt;&lt; std::is_rvalue_reference&lt;T&gt;::value &lt;&lt; &quot;\n&quot;;
}

int main()
{
    int x = 0;
    test(x);
    test(x + 1);
    return 0;
}
</code></pre>
<p>Gibt bei mir immer &quot;is rvalue: 0&quot; aus.<br />
Geht das generell so nicht (mache ich was falsch),<br />
oder liegt es vielleicht an meinen compiler (g++ 4.5)?</p>
<p>thx <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136014</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136014</guid><dc:creator><![CDATA[Plassy]]></dc:creator><pubDate>Wed, 26 Oct 2011 10:46:52 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 11:02:06 GMT]]></title><description><![CDATA[<p>Probiers mal mit <code>T&amp;&amp;</code> als Parameter.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136025</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136025</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 26 Oct 2011 11:02:06 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 11:03:25 GMT]]></title><description><![CDATA[<p>Ist ja auch irgendwie logisch. Du übergibst kein RValue.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136029</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136029</guid><dc:creator><![CDATA[Mentras]]></dc:creator><pubDate>Wed, 26 Oct 2011 11:03:25 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 11:18:38 GMT]]></title><description><![CDATA[<p>Mentras schrieb:</p>
<blockquote>
<p>Ist ja auch irgendwie logisch. Du übergibst kein RValue.</p>
</blockquote>
<p>Woher weist du das T nicht int&amp;&amp; ist?<br />
&quot;test&lt;int&amp;&amp;&gt;(x + 1);&quot; funktioniert wunderbar.<br />
Ich hab eigendlich angenommen das der compiler von selbst darauf kommt das test(x + 1) eine rvalue refernce übergeben wird und somit test(int&amp;&amp;) wird.</p>
<p>Ich hab wohl auch nicht deutlich genug gefragt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> .<br />
Also sollte der compiler von selbst sehen das T ne rvalue reference ist oder geht das generell so nicht (wobei ich dann auch gerne mal wissen würde wozu std::is_rvalue_reference&lt;..&gt; eigendlich gut sein sollte)?</p>
<p>thx</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136035</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136035</guid><dc:creator><![CDATA[Plassy]]></dc:creator><pubDate>Wed, 26 Oct 2011 11:18:38 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 11:23:53 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Probiers mal mit <code>T&amp;&amp;</code> als Parameter.</p>
</blockquote>
<p>thx <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /> so gehts... verstehe die logik hinder dieser syntax nur nicht ganz?<br />
<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136038</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136038</guid><dc:creator><![CDATA[Plassy]]></dc:creator><pubDate>Wed, 26 Oct 2011 11:23:53 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 11:26:21 GMT]]></title><description><![CDATA[<p>Also so funktionierts:</p>
<pre><code class="language-cpp">#include &lt;type_traits&gt;
#include &lt;iostream&gt;

template&lt;class T&gt;
void test(T&amp;&amp;)
{
    std::cout &lt;&lt; &quot;is rvalue: &quot; &lt;&lt; std::is_rvalue_reference&lt;T&amp;&amp;&gt;::value &lt;&lt; &quot;\n&quot;;
}

int main()
{
    int x = 0;
    test(x);
    test(x + 1);
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2136041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136041</guid><dc:creator><![CDATA[Plassy]]></dc:creator><pubDate>Wed, 26 Oct 2011 11:26:21 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 11:37:47 GMT]]></title><description><![CDATA[<p>RValue Referenzen sind übrigens LValues.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136043</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Wed, 26 Oct 2011 11:37:47 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 11:48:52 GMT]]></title><description><![CDATA[<p>Plassy schrieb:</p>
<blockquote>
<p>Woher weist du das T nicht int&amp;&amp; ist?</p>
</blockquote>
<p>Ist es nie, <code>T</code> alleine wird immer als Werttyp deduziert. Genauso wird <code>T</code> nie als <code>int&amp;</code> aufgefasst.</p>
<p>Plassy schrieb:</p>
<blockquote>
<p>verstehe die logik hinder dieser syntax nur nicht ganz?</p>
</blockquote>
<p>Die Semantik von RValue-Referenz-Parametern ist bei Funktionstemplates anders. Das ist für sogenanntes &quot;Perfect Forwarding&quot; notwendig und stellt eine super-unintuitive Sonderregel dar, siehe z.B. <a href="http://www.c-plusplus.net/forum/p2022463#2022463" rel="nofollow">hier</a>.</p>
<p>314159265358979 schrieb:</p>
<blockquote>
<p>RValue Referenzen sind übrigens LValues.</p>
</blockquote>
<p>Um Verwirrung zu vermeiden: Alle benannten Variablen (auch RValue-Referenzen) sind LValues.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136056</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136056</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 26 Oct 2011 11:48:52 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 11:51:05 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Genauso wird <code>T</code> nie als <code>int&amp;</code> aufgefasst.</p>
</blockquote>
<p>Wenn man einen LValue übergibt, dann schon. Denn dann wird T = U&amp; und T&amp;&amp; = U&amp;.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136057</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136057</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Wed, 26 Oct 2011 11:51:05 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 11:59:48 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Wenn man einen LValue übergibt, dann schon. Denn dann wird T = U&amp; und T&amp;&amp; = U&amp;.</p>
</blockquote>
<p>Nein, bei</p>
<pre><code class="language-cpp">template &lt;typename T&gt; 
void Fn(T) 
{ 
	static_assert(!std::is_lvalue_reference&lt;T&gt;::value, &quot;impossible&quot;); 
}
</code></pre>
<p>wird die Static-Assertion nie fehlschlagen, sofern man das Template-Argument nicht explizit angibt.</p>
<p>Nur bei <code>T&amp;&amp;</code> sind die Regeln komisch. Dafür dann aber richtig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136067</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136067</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 26 Oct 2011 11:59:48 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 12:03:58 GMT]]></title><description><![CDATA[<p>Achso, okay, dann war das ein Missverständnis. Ich war gerade bei der Funktion mit T&amp;&amp;.</p>
<p>Nexus schrieb:</p>
<blockquote>
<p>Um Verwirrung zu vermeiden: Alle benannten Variablen (auch RValue-Referenzen) sind LValues.</p>
</blockquote>
<p>Nochmal bisschen ausführlicher: Alles was einen Namen hat, ist <strong>in jedem Fall</strong> ein LValue. Es gibt aber auch unbenannte LValues.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136070</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136070</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Wed, 26 Oct 2011 12:03:58 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 12:11:01 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>RValue Referenzen sind LValues.</p>
</blockquote>
<p>Das kann man so pauschal nicht sagen. std::move gibt eine Rvalue-Referenz zurück. Ein solcher Aufruf ist aber kein Lvalue.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/885">@Plassy</a>:<br />
Die ganze Lvalue/Rvalue Sache und mit den Referenzen kann man leider nicht mit einem Satz erklären. Ich habe auch eine Weile gebraucht, das alles zu verstehen.</p>
<p>Hauptsächlich unterscheiden sich die Referenztypen dadurch, wie sie sich initialisieren lassen. non-const-Lvalue-Referenzen lassen sich nur mit Lvalues (oder etwas, &quot;was zu einem Lvalue konvertierbar ist&quot;) initialisieren. Rvalue-Referenzen lassen sich nur entweder mit Rvalues initialisieren, oder zumindest so, dass die entstandende Referenz auf ein temporäres Objekt zeigt.</p>
<p>Darüberhinaus ist ein Aufruf einer Funktion, welche eine Rvalue-Referenz zurückgibt, selbst ein Rvalue. <em>Benannte</em> Rvalue-Referenzen verhalten sich aber genau so wie Lvalue-Referenzen. Das ist Absicht und auch gut so.</p>
<p>Das Kapitel &quot;Template-Argument-Deduction&quot; ist auch nicht trivial. Das ist ein Haufen von Regeln, die sagen, was in den einzelnen Fällen passieren soll. Beispielsweise wird bei</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
void foo(T);
</code></pre>
<p>das T <em>nie</em> zu einem Referenztyp deduziert. Der Typ eines Ausdrucks ist auch nie eine Referenz. Also:</p>
<pre><code class="language-cpp">int main() {
  int  i = 23;
  int&amp; r = i;
}
</code></pre>
<p>Hier unterscheiden sich i und r in ihren <em>deklarierten Typen</em>. Wenn Du aber r als Teilausdruck irgendwo verwendest, ist der Typ dieses Teilausdrucks einfach <code>int</code> .</p>
<p>Damit man in generischem Code (zwecks &quot;perfect forwarding&quot;) Lvalues und Rvalues unterscheiden kann, hat man sich überlegt, eine Sonderregel bei der Template-Argument-Deduction einzuführen:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
void foo(T&amp;&amp;);
//       ^^^    &lt;-- Die &quot;Ich-fresse-alles-und-merke-mir-
//                  die-L/Rvalueness-im-Typ-Signatur&quot;

int main() {
  int i = 23;
  foo(i+1);   // T=int , T&amp;&amp;=int&amp;&amp;
  foo(i);     // T=int&amp;, T&amp;&amp;=int&amp;
  //               ^^^^
  // Besondere Deduktionsregel bei Lvalue-Argument für &quot;T&amp;&amp;&quot;-Parameter
}
</code></pre>
<p>Dass mit T=int&amp; bei T&amp;&amp; auch int&amp; rauskommt, ist das Ergebnis der &quot;Referenz-Collapsing-Regeln&quot;. Eine Referenz auf eine Referenz macht nämlich gar keinen Sinn. Die Lvalue-Referenz drückt sich quasi immer durch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136073</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136073</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Wed, 26 Oct 2011 12:11:01 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 12:22:30 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>Das kann man so pauschal nicht sagen. std::move gibt eine Rvalue-Referenz zurück. Ein solcher Aufruf ist aber kein Lvalue.</p>
</blockquote>
<p>Du hast Recht, danke für den Hinweis. Es ist ein XValue.<br />
Edit: Und als solcher auch ein GLValue. Also lag ich gar nicht mal so falsch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136081</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136081</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Wed, 26 Oct 2011 12:22:30 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 12:22:28 GMT]]></title><description><![CDATA[<p>Dazu fällt mir nur eins ein: wer sich x-, pr- und gl-values ausgedacht hat, hat jedenfalls das Recht verwirkt, jemand wegen Variablennamen wie i und j zu kritisieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136086</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136086</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 26 Oct 2011 12:22:28 GMT</pubDate></item><item><title><![CDATA[Reply to mit type traits auf rvalue testen? on Wed, 26 Oct 2011 12:46:43 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/885">@Plassy</a>:<br />
Die ganze Lvalue/Rvalue Sache und mit den Referenzen kann man leider nicht mit einem Satz erklären. Ich habe auch eine Weile gebraucht, das alles zu verstehen.<br />
...</p>
</blockquote>
<p>Ah... danke für die ausführliche erklärung :).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2136102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2136102</guid><dc:creator><![CDATA[Plassy]]></dc:creator><pubDate>Wed, 26 Oct 2011 12:46:43 GMT</pubDate></item></channel></rss>