Guaranteed lifetime of temporary in C++?
<p>Does C++ provide a guarantee for the lifetime of a temporary variable that is created within a function call but not used as a parameter?  Here's an example class:</p>

<pre><code>class StringBuffer
{
public:
    StringBuffer(std::string &amp; str) : m_str(str)
    {
        m_buffer.push_back(0);
    }
    ~StringBuffer()
    {
        m_str = &amp;m_buffer[0];
    }
    char * Size(int maxlength)
    {
        m_buffer.resize(maxlength + 1, 0);
        return &amp;m_buffer[0];
    }
private:
    std::string &amp; m_str;
    std::vector&lt;char&gt; m_buffer;
};
</code></pre>

<p>And here's how you would use it:</p>

<pre><code>// this is from a crusty old API that can't be changed
void GetString(char * str, int maxlength);

std::string mystring;
GetString(StringBuffer(mystring).Size(MAXLEN), MAXLEN);
</code></pre>

<p>When will the destructor for the temporary StringBuffer object get called?  Is it:</p>

<ul>
<li>Before the call to GetString?</li>
<li>After GetString returns?</li>
<li>Compiler dependent?</li>
</ul>

<p>I know that C++ guarantees that a local temporary variable will be valid as long as there's a reference to it - does this apply to parent objects when there's a reference to a member variable?</p>

<p>Thanks.</p>
