<?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[union member assignment]]></title><description><![CDATA[<p>hallo</p>
<pre><code class="language-cpp">union type
{
  int x;
  float y;
};

int main()
{
  type val;
  val.x = 1;

//sicher ok:
  float tmp = static_cast&lt;float&gt;( val.x );
  val.y = tmp;

//auch ok, oder eine schlechte idee?
  val.y = static_cast&lt;float&gt;( val.x );
}
</code></pre>
<p>ty, bb</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/331005/union-member-assignment</link><generator>RSS for Node</generator><lastBuildDate>Thu, 02 Jul 2026 17:51:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/331005.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Feb 2015 19:39:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to union member assignment on Thu, 05 Feb 2015 19:39:20 GMT]]></title><description><![CDATA[<p>hallo</p>
<pre><code class="language-cpp">union type
{
  int x;
  float y;
};

int main()
{
  type val;
  val.x = 1;

//sicher ok:
  float tmp = static_cast&lt;float&gt;( val.x );
  val.y = tmp;

//auch ok, oder eine schlechte idee?
  val.y = static_cast&lt;float&gt;( val.x );
}
</code></pre>
<p>ty, bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441436</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441436</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Thu, 05 Feb 2015 19:39:20 GMT</pubDate></item><item><title><![CDATA[Reply to union member assignment on Thu, 05 Feb 2015 19:57:40 GMT]]></title><description><![CDATA[<p>unskilled schrieb:</p>
<blockquote>
<p>h val.y = tmp;</p>
<p>//auch ok, oder eine schlechte idee?<br />
val.y = static_cast&lt;float&gt;( val.x );</p>
</blockquote>
<p>Nicht ok. Das ist undefiniert.</p>
<p>Bei union gibt es maximal einen aktiven Member (der, dem zuletzt etwas zugewiesen wurde) und nur dieser darf gelesen werden. Die anderen Member zu lesen führt zu undefiniertem Verhalten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441440</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441440</guid><dc:creator><![CDATA[tybb]]></dc:creator><pubDate>Thu, 05 Feb 2015 19:57:40 GMT</pubDate></item><item><title><![CDATA[Reply to union member assignment on Thu, 05 Feb 2015 20:03:44 GMT]]></title><description><![CDATA[<blockquote>
<p>Die anderen Member zu lesen führt zu undefiniertem Verhalten</p>
</blockquote>
<p>Wir lesen nur einen Member.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441441</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441441</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Thu, 05 Feb 2015 20:03:44 GMT</pubDate></item><item><title><![CDATA[Reply to union member assignment on Thu, 05 Feb 2015 20:05:37 GMT]]></title><description><![CDATA[<p>Arcoth schrieb:</p>
<blockquote>
<blockquote>
<p>Die anderen Member zu lesen führt zu undefiniertem Verhalten</p>
</blockquote>
<p>Wir lesen nur einen Member.</p>
</blockquote>
<p>Was ist an meinem Satz in diesem Zusammenhang missverständlich?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441442</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441442</guid><dc:creator><![CDATA[tybb]]></dc:creator><pubDate>Thu, 05 Feb 2015 20:05:37 GMT</pubDate></item><item><title><![CDATA[Reply to union member assignment on Thu, 05 Feb 2015 20:33:54 GMT]]></title><description><![CDATA[<p>AFAICS ist es leider nicht definiert:</p>
<p>[expr.ass]/8 schrieb:</p>
<blockquote>
<p>If the value being stored in an object is accessed from another object that overlaps in any way the storage of the first object, then the overlap shall be exact <strong>and the two objects shall have the same type, otherwise the behavior is undefined.</strong></p>
</blockquote>
<p>Hier ist eine Notiz aus <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#556" rel="nofollow">DR #556</a>:</p>
<blockquote>
<p>Instead of being a general statement about aliasing, it's describing the situation in which the source of the value being assigned is storage that overlaps the storage of the target object.</p>
</blockquote>
<p>Andererseits führt <code>static_cast</code> eine Temporäre Variable ein:</p>
<blockquote>
<p>An expression <code>e</code> can be explicitly converted to a type <code>T</code> using a <code>static_cast</code> of the form <code>static_cast&lt;T&gt;(e)</code> if the declaration <code>T t(e);</code> is well-formed, for some invented temporary variable <code>t</code> (8.5). The effect of such an explicit conversion is the same as performing the declaration and initialization and then using the temporary<br />
variable as the result of the conversion.</p>
</blockquote>
<p>Diese temporäre Variable wäre natürlich völlig unabhängig von diesem Unionobjekt..</p>
<blockquote>
<p>Was ist an meinem Satz in diesem Zusammenhang missverständlich?</p>
</blockquote>
<p>Entweder der Satz ist einfach total zusammenhangslos von dir reingeworfen worden, oder er hält als Begründung für den ersten her, und den Fall habe ich angenommen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441443</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441443</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Thu, 05 Feb 2015 20:33:54 GMT</pubDate></item><item><title><![CDATA[Reply to union member assignment on Fri, 06 Feb 2015 07:29:18 GMT]]></title><description><![CDATA[<p>@tybb natürlich ist die zweite variante eine alternative zur ersten. offenbar war das doof geschrieben, deshalb wohl deine antwort</p>
<p>@arcoth: danke; dann werd ichs wohl doch mit der temporären variable machen. wird schon wegoptimiert werden, wenns nicht nötig ist.</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2441501</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2441501</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Fri, 06 Feb 2015 07:29:18 GMT</pubDate></item></channel></rss>