Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22067

There's one constructor, but two destructors [duplicate]

$
0
0

It took me some time to get the error but still can't know how to solve it

As it appears at the code there's only one constructor call but it calls the destructor twice

Code:

struct A
{
  A()
  {
      std::cout << "C'tor"<< std::endl;
  }

  A(A&&)
  {
      std::cout << "M'tor"<< std::endl;
  }

  A(const A&) = delete;
  A& operator=(const A&) = delete;
  A& operator=(A&&) = delete;

  ~A()
  {
      std::cout << "D'tor"<< std::endl;
  }
};

int main()
{
    auto lambda = [](int i){
        A a;
        if (i == 0)
            return std::move(a);
    };

    lambda(1);
    return 0;
}

Output:

C'tor                                                                                                                                                                        
D'tor                                                                                                                                                                        
D'tor

How can that happen? should the compiler could at the very least generate a warning for me? What do you think?


Viewing all articles
Browse latest Browse all 22067

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>