I recently played with Explorer Compiler . I downloaded one of my examples, which takes pointer parameters and changes it, take unique_ptr parameters instead. But I noticed that in the output assembly the operator deletion calls were noticeably absent. I am wondering if anyone knows why.
Here is an example that you can insert into Explorer. Be sure to add -O3 to the compiler options.
#include <memory> using std::unique_ptr; void maxArray(unique_ptr<double[]> x, unique_ptr<double[]> y) { for (int i = 0; i < 65536; i++) { if (y[i] > x[i]) x[i] = y[i]; } }
EDIT: Also for comparison, if instead I paste one of the sample code from cppreference, then I get the delete operator in the output file.
#include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo::Foo\n"; } ~Foo() { std::cout << "Foo::~Foo\n"; } void bar() { std::cout << "Foo::bar\n"; } }; void f(const Foo &) { std::cout << "f(const Foo&)\n"; } int main() { std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo if (p1) p1->bar(); { std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo f(*p2); p1 = std::move(p2); // ownership returns to p1 std::cout << "destroying p2...\n"; } if (p1) p1->bar(); // Foo instance is destroyed when p1 goes out of scope }
EDIT: +1 to krzaq. It is the caller, not the caller, that builds and destroys the parameters.
source share