title: c++, object life-time of anonymous (unnamed) variables
body: <p>In the following code, the object constructed in the last line of 'main()', seems to be destroyed before the end of the expression. The destructor is called before the '&lt;&lt;' is executed. Is this how it is supposed to be?</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;iostream&gt;
  
  using std::string;
  using std::ostringstream;
  using std::cout;
  
  class A : public ostringstream
  {
  public:
    A () {}
    virtual ~A ()
    {    
      string s;
      s = str();
      cout &lt;&lt; "from A: " &lt;&lt; s &lt;&lt; std::endl;
    }
  };
  
  int
  main ()
  {
    string s = "Hello";
    A os;
  
    os &lt;&lt; s;
    cout &lt;&lt; os.str() &lt;&lt; std::endl;
  
    A() &lt;&lt; "checking this";
  }
  </code></pre>
  
  <p>This is the output:</p>
  
  <pre><code>Hello
  from A: 0x80495f7
  from A: Hello
  </code></pre>
  
  <p>This is the gdb log:</p>
  
  <pre><code>(gdb) b os.cxx : 18
  Breakpoint 1 at 0x80492b1: file os.cxx, line 18. (2 locations)
  (gdb) r
  Starting program: /home/joe/sandbox/test/os 
  Hello
  
  Breakpoint 1, ~A (this=0xbffff37c, __in_chrg=&lt;value optimized out&gt;, __vtt_parm=&lt;value optimized out&gt;) at os.cxx:18
  18     cout &lt;&lt; "from A: " &lt;&lt; s &lt;&lt; std::endl;
  (gdb) p s.c_str ()
  $1 = 0x804b45c "0x80495f7"
  (gdb) p *s.c_str ()
  $2 = 48 '0'
  (gdb) c
  Continuing.
  from A: 0x80495f7
  
  Breakpoint 1, ~A (this=0xbffff2bc, __in_chrg=&lt;value optimized out&gt;, __vtt_parm=&lt;value optimized out&gt;) at os.cxx:18
  18     cout &lt;&lt; "from A: " &lt;&lt; s &lt;&lt; std::endl;
  (gdb) p s.c_str ()
  $3 = 0x804b244 "Hello"
  (gdb) p *s.c_str ()
  $4 = 72 'H'
  (gdb) c
  Continuing.
  from A: Hello
  
  Program exited normally.
  (gdb)
  </code></pre>