Does the compiler optimize the return value in the case of the returned member variable?

Given the following code

class foo
{
    private:
        boost::shared_ptr <std::deque<foo> > m_ptr;
    public:
        foo();
        boost::shared_ptr <std::deque<foo> > get_my_ptr()
        {
            return m_ptr;
        }
};

And when we call a get_my_ptr()function like this

boost::shared_ptr <std::deque<foo> > ptr = get_my_ptr()

Does the copy constructor make a compiler call to create the ptr object, or can it execute nrvo? And what is the difference that we call so

const boost::shared_ptr <std::deque<foo> >& ptr = get_my_ptr()
+4
source share
2 answers

NRVO , return - , , . , . -. , :

boost::shared_ptr<std::deque<foo>> get_my_ptr()
{
    auto p = m_ptr;
    return p;
}

ctor, . , .

+3

RVO -. , .

+2

Source: https://habr.com/ru/post/1628074/


All Articles