Monday, November 13, 2017

C++17: std::destroy_at

C++17 added std::destroy_at() that can call the destructor of an object. Here is an example:

#include <memory>

int main()
{
  int * pInt = new int();

  *pInt = 1;

  std::destroy_at(pInt); // Destructor called, but 
                         //   memory is still allocated.

  *pInt = 2;

  delete pInt; // Destructor called, and memory is deallocated.

  // *pInt = 3; // Exception Thrown (in Debug Mode)

  return 0;
}
Reference: http://en.cppreference.com/w/cpp/memory/destroy_at

No comments:

Post a Comment