Guaranteed lifetime of temporary in C++?

后端 未结 5 2021
天涯浪人
天涯浪人 2020-11-22 12:22

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:

相关标签:
5条回答
  • 2020-11-22 12:40

    After the call to GetString returns.

    0 讨论(0)
  • 2020-11-22 12:46

    The destructor for that sort of temporaries is called at the end of the full-expression. That's the most outer expression which is not part of any other expression. That is in your case after the function returns and the value is evaluated. So, it will work all nice.

    It's in fact what makes expression templates work: They can keep hold references to that sort of temporaries in an expression like

    e = a + b * c / d
    

    Because every temporary will last until the expression

    x = y
    

    Is evaluated completely. It's quite concisely described in 12.2 Temporary objects in the Standard.

    0 讨论(0)
  • 2020-11-22 12:51

    StringBuffer is in the scope of GetString. It should get destroyed at the end of GetString's scope (ie when it returns). Also, I don't believe that C++ will guarantees that a variable will exist as long as there is reference.

    The following ought to compile:

    Object* obj = new Object;
    Object& ref = &(*obj);
    delete obj;
    
    0 讨论(0)
  • 2020-11-22 12:56

    litb's answer is accurate. The lifetime of the temporary object (also known as an rvalue) is tied to the expression and the destructor for the temporary object is called at the end of the full expression and when the destructor on StringBuffer is called, the destructor on m_buffer will also be called, but not the destructor on m_str since it is a reference.

    Note that C++0x changes things just a little bit because it adds rvalue references and move semantics. Essentially by using an rvalue reference parameter (notated with &&) I can 'move' the rvalue into the function (instead of copying it) and the lifetime of the rvalue can be bound to the object it moves into, not the expression. There is a really good blog post from the MSVC team on that walks through this in great detail and I encourage folks to read it.

    The pedagogical example for moving rvalue's is temporary strings and I'll show assignment in a constructor. If I have a class MyType that contains a string member variable, it can be initialized with an rvalue in the constructor like so:

    class MyType{
       const std::string m_name;
    public:
       MyType(const std::string&& name):m_name(name){};
    }
    

    This is nice because when I declare an instance of this class with a temporary object:

    void foo(){
        MyType instance("hello");
    }
    

    what happens is that we avoid copying and destroying the temporary object and "hello" is placed directly inside the owning class instance's member variable. If the object is heavier weight than a 'string' then the extra copy and destructor call can be significant.

    0 讨论(0)
  • 2020-11-22 12:57

    I wrote almost exactly the same class:

    template <class C>
    class _StringBuffer
    {
        typename std::basic_string<C> &m_str;
        typename std::vector<C> m_buffer;
    
    public:
        _StringBuffer(std::basic_string<C> &str, size_t nSize)
            : m_str(str), m_buffer(nSize + 1) { get()[nSize] = (C)0; }
    
        ~_StringBuffer()
            { commit(); }
    
        C *get()
            { return &(m_buffer[0]); }
    
        operator C *()
            { return get(); }
    
        void commit()
        {
            if (m_buffer.size() != 0)
            {
                size_t l = std::char_traits<C>::length(get());
                m_str.assign(get(), l);    
                m_buffer.resize(0);
            }
        }
    
        void abort()
            { m_buffer.resize(0); }
    };
    
    template <class C>
    inline _StringBuffer<C> StringBuffer(typename std::basic_string<C> &str, size_t nSize)
        { return _StringBuffer<C>(str, nSize); }
    

    Prior to the standard each compiler did it differently. I believe the old Annotated Reference Manual for C++ specified that temporaries should clean up at the end of the scope, so some compilers did that. As late as 2003, I found that behaviour still existed by default on Sun's Forte C++ compiler, so StringBuffer didn't work. But I'd be astonished if any current compiler was still that broken.

    0 讨论(0)
提交回复
热议问题