<?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[The secret of definitions in case]]></title><description><![CDATA[<p>Hi,</p>
<p>ich dachte immer, Definition wären in case-Bereich verboten, da man ansonsten paar Probleme micht Sichtbarkeiten und Gültigkeitsbereichen bekommen könnte. Folgedessen muss man solche in Anweisungsblöcke packen.</p>
<p>Doch heute wurde ich vom Gegenteil überzeugt:</p>
<pre><code class="language-cpp">// Kompiliert
switch(var)
{
    case 1: int a; break;
    case 2: a = 123;
}
</code></pre>
<pre><code class="language-cpp">// Kompiliert nat. nicht
switch(var)
{
    case 1: { int a; } break;
    case 2: a = 123;
}
</code></pre>
<pre><code class="language-cpp">// Kompiliert nicht
switch(var)
{
    case 1: string a; break;
    case 2: a = &quot;Hallo&quot;;
}
</code></pre>
<pre><code class="language-cpp">// Kompiliert
class A { };

switch(var)
{
    case 1: A a; break;
    case 2: a = A();
}
</code></pre>
<pre><code class="language-cpp">// Huch, kompiliert nicht
class A { public: A() {} };

switch(var)
{
    case 1: A a; break;
    case 2: a = A();
}
</code></pre>
<p>???</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/222660/the-secret-of-definitions-in-case</link><generator>RSS for Node</generator><lastBuildDate>Sun, 04 Oct 2026 22:43:16 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/222660.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 15 Sep 2008 19:32:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to The secret of definitions in case on Mon, 15 Sep 2008 19:32:57 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ich dachte immer, Definition wären in case-Bereich verboten, da man ansonsten paar Probleme micht Sichtbarkeiten und Gültigkeitsbereichen bekommen könnte. Folgedessen muss man solche in Anweisungsblöcke packen.</p>
<p>Doch heute wurde ich vom Gegenteil überzeugt:</p>
<pre><code class="language-cpp">// Kompiliert
switch(var)
{
    case 1: int a; break;
    case 2: a = 123;
}
</code></pre>
<pre><code class="language-cpp">// Kompiliert nat. nicht
switch(var)
{
    case 1: { int a; } break;
    case 2: a = 123;
}
</code></pre>
<pre><code class="language-cpp">// Kompiliert nicht
switch(var)
{
    case 1: string a; break;
    case 2: a = &quot;Hallo&quot;;
}
</code></pre>
<pre><code class="language-cpp">// Kompiliert
class A { };

switch(var)
{
    case 1: A a; break;
    case 2: a = A();
}
</code></pre>
<pre><code class="language-cpp">// Huch, kompiliert nicht
class A { public: A() {} };

switch(var)
{
    case 1: A a; break;
    case 2: a = A();
}
</code></pre>
<p>???</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1582457</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1582457</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 15 Sep 2008 19:32:57 GMT</pubDate></item><item><title><![CDATA[Reply to The secret of definitions in case on Mon, 15 Sep 2008 19:59:59 GMT]]></title><description><![CDATA[<p>lustig</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1582483</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1582483</guid><dc:creator><![CDATA[vlad_tepesch]]></dc:creator><pubDate>Mon, 15 Sep 2008 19:59:59 GMT</pubDate></item><item><title><![CDATA[Reply to The secret of definitions in case on Mon, 15 Sep 2008 20:05:22 GMT]]></title><description><![CDATA[<p>Mit welchem Compiler denn?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1582491</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1582491</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Mon, 15 Sep 2008 20:05:22 GMT</pubDate></item><item><title><![CDATA[Reply to The secret of definitions in case on Mon, 15 Sep 2008 20:07:04 GMT]]></title><description><![CDATA[<p>Badestrand schrieb:</p>
<blockquote>
<p>Mit welchem Compiler denn?</p>
</blockquote>
<p>Comeau und g++.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1582492</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1582492</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Mon, 15 Sep 2008 20:07:04 GMT</pubDate></item><item><title><![CDATA[Reply to The secret of definitions in case on Mon, 15 Sep 2008 20:29:56 GMT]]></title><description><![CDATA[<p>Mit dem MSVC++05 geht's auch, merkwürdig. Gut, immerhin nur PODs aber sowas ist man ja nicht gewohnt vom Standard (oder welchen Sinn hat das, der sich mir nicht erschließt?) <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1582514</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1582514</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Mon, 15 Sep 2008 20:29:56 GMT</pubDate></item><item><title><![CDATA[Reply to The secret of definitions in case on Mon, 15 Sep 2008 20:42:42 GMT]]></title><description><![CDATA[<p>Wirklich interessant, ich habe aber gerade mal den Standard gewälzt und da sieht man auch die Erklärung. Die beiden interessanten Stellen sind</p>
<p>Standard, 6.7.3 schrieb:</p>
<blockquote>
<p>It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. <strong>A program that jumps (77) from a point where a local variable with automatic storage duration is not in scope to a point where it is in scope is illformed</strong> <em>unless the variable has POD type (3.9) and is declared without an initializer (8.5)</em>.</p>
<p>(77) The transfer from the condition of a switch statement to a case label is considered a jump in this respect</p>
</blockquote>
<p>und (aber nicht mehr ganz so interessant):</p>
<p>Standard, C.1.4 schrieb:</p>
<blockquote>
<p>C.1.4 Clause 6: statements [diff.stat]<br />
6.4.2, 6.6.4 (switch and goto statements)<br />
1 Change: It is now invalid to jump past a declaration with explicit or implicit initializer (except across entire block not entered) Rationale: Constructors used in initializers may allocate resources which need to be deallocated upon leaving the block. Allowing jump past initializers would require complicated runtime determination of allocation. Furthermore, any use of the uninitialized object could be a disaster. With this simple compiletime rule, C++ assures that if an initialized variable is in scope, then it has assuredly been initialized. Effect on original feature: Deletion of semantically welldefined feature.<br />
Difficulty of converting: Semantic transformation.<br />
How widely used: Seldom.</p>
</blockquote>
<p>Felix</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1582522</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1582522</guid><dc:creator><![CDATA[Phoemuex]]></dc:creator><pubDate>Mon, 15 Sep 2008 20:42:42 GMT</pubDate></item><item><title><![CDATA[Reply to The secret of definitions in case on Tue, 16 Sep 2008 22:43:49 GMT]]></title><description><![CDATA[<p>Danke für die Std-Stelle.<br />
Stranges Verhalten, aber hat bestimmt auch seinen Grund.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1583154</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1583154</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 16 Sep 2008 22:43:49 GMT</pubDate></item><item><title><![CDATA[Reply to The secret of definitions in case on Wed, 17 Sep 2008 02:44:41 GMT]]></title><description><![CDATA[<p>was mit switch alles geht ist schon irgendwie &quot;krank&quot;. man denke nur an duffs device.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1583182</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1583182</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 17 Sep 2008 02:44:41 GMT</pubDate></item></channel></rss>