Does unique_ptr not generate a delete command in Compiler Explorer?

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.

+5
source share
1 answer

This problem boils down to the following:

 void foo(unique_ptr<int>) { } foo(make_unique<int>()); 

Where is the foo parameter located?

The following standard is relevant in this question:

N4140 ยง5.2.2 [expr.call] / 4

The parameter's lifetime expires when the function in which it is found returns certain. The initialization and destruction of each parameter occurs in the context of the calling function.

In other words: your maxArray not required to be responsible for calling x and y destructors; gcc implements it this way, and therefore there are no delete calls in the encoder.

+6
source

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


All Articles