Does C++ require a destructor call for each placement new?
Posted
by
Josh Haberman
on Stack Overflow
See other posts from Stack Overflow
or by Josh Haberman
Published on 2011-01-02T22:56:12Z
Indexed on
2011/01/03
8:53 UTC
Read the original article
Hit count: 188
c++
I understand that placement new calls are usually matched with explicit calls to the destructor. My question is: if I have no need for a destructor (no code to put there, and no member variables that have destructors) can I safely skip the explicit destructor call?
Here is my use case: I want to write C++ bindings for a C API. In the C API many objects are accessible only by pointer. Instead of creating a wrapper object that contains a single pointer (which is wasteful and semantically confusing). I want to use placement new to construct an object at the address of the C object. The C++ object will do nothing in its constructor or destructor, and its methods will do nothing but delegate to the C methods. The C++ object will contain no virtual methods.
I have two parts to this question.
Is there any reason why this idea will not work in practice on any production compiler?
Does this technically violate the C++ language spec?
© Stack Overflow or respective owner