assign variable to itself on declaration
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/981714/is-self-initialization-a-a-a-allowed">Is self-initialization &#39;A a = a;&#39; allowed?</a>  </p>
</blockquote>



<p>Consider the following code:</p>

<pre><code>int main()
{
  int x = x;
  return 0;
}
</code></pre>

<p>Does it compile? Well, I thought it wouldn't, but it does according to my <em>gcc-4.2.1</em>. The value is unspecified, as it simply would state <code>int x;</code>.</p>

<p>It has some non-trivial consequences though, for example:</p>

<pre><code>int x = 44;
int main()
{
  int x = x;
  return 0;
}
</code></pre>

<p>The <code>x</code> in <code>main</code> is still unspecified, as the third <code>x</code> is the second (I know I can overcome it by using <code>::x</code>, but it's not the point).</p>

<p>One can argue <code>int x = x;</code> in this case is equivalent to <code>int x; x = x;</code>, but things are getting more complicated when using a proper object:</p>

<pre><code>class S
{
public:
  S(): x(44) { std::cout &lt;&lt; "constructor" &lt;&lt; std::endl; }  
  S(const S &amp;s): x(s.x) 
             { std::cout &lt;&lt; "copy " &lt;&lt; this &lt;&lt; " " &lt;&lt; &amp;s &lt;&lt; std::endl; }

   int getx () const { return x; }

private:
  int x;  
};

int main()
{
  S s(s);
  std::cout &lt;&lt; s.getx() &lt;&lt; std::endl;
  return 0;
}
</code></pre>

<p>The code above prints:</p>

<pre><code>copy 0x7fff65ac1598 0x7fff65ac1598
0
</code></pre>

<p>So obviously object passed to copy constructor is an unconstructed bag of garbage (which is not surprising if able to compile).</p>

<p>I don't get any warnings while compiling, even when using <code>-Wall -Wextra</code>. Is it a bug in <em>gcc</em>, completely valid behaviour or kind of a grey area?</p>

<p>(One way or another, it's really pain to debug.)</p>
