// Destructor. If there is a C object, delete it.
// We don't need to test ptr_ == NULL because C++ does that for us
~scoped_ptr() {
enum { type_must_be_complete = sizeof(C) };
delete ptr_;
}
Note: C is a template parameter
I know we cant delete a null pointer, an exception will be raised.
So in this case, the enum definition must be doing something to prevent that.
In production, sometimes we dont want to end a program simple because we have a null pointer, we may want to look at alternative scenario, when the pointer is null.
And this code is used in production, almost everywhere?
Thanks guys.
it’s effectively a static assertion for the deletion. the implementation wants to know if it is dealing with a type whose declaration is visible before deleting the variable, rather than a forward declaration.
Your compiler will emit an error when you ask it the size of an incomplete type:
Update
As your compiler and Matthieu M. will tell you —
delete-ing an incomplete type is undefined.