Hacker News new | ask | show | jobs
by fabriceleal 4794 days ago
C++ noob here. This reminded me of "Obscure C++ Features" [1] (discussion [2]) - the 'Placement new' feature. Basically, we can allocate memory using malloc and then call the constructor on the allocated memory:

  // Must allocate our own memory
  Test *ptr = (Test *)malloc(sizeof(Test));

  // Use placement new
  new (ptr) Test;

  // Must call the destructor ourselves
  ptr->~Test();

  // Must release the memory ourselves
  free(ptr);

My point is: is possible that the explicit constructor call feature is intended to be used in this use case?

[1]: http://madebyevan.com/obscure-cpp-features/

[2]: https://news.ycombinator.com/item?id=5577631