Categories
Development

unique_ptr: what is the effect of calling reset before assignment

In c++14, what is the difference between assigning to an existing unique_ptr:

    std::unique_ptr<double> p = std::make_unique<double>(1.0);
    p = std::make_unique<double>(2.0);

and first calling reset before assigning to it:?

    std::unique_ptr<double> p = std::make_unique<double>(1.0);
    p.reset();
    p = std::make_unique<double>(2.0);

I thought they would be equivalent, but the one without reset() seems to produce different (and more) assembly code when compiled with -O3:

https://godbolt.org/z/mBApWH

https://godbolt.org/z/JfStmC

Leave a Reply

Your email address will not be published. Required fields are marked *