To understand the enum
, start with considering the destructor without it:
~scoped_ptr() {
delete ptr_;
}
where ptr_
is a C*
. If type C
is incomplete at this point, i.e. all that the compiler knows is struct C;
, then (1)a default-generated do-nothing destructor is used for the C instance pointed to. That's unlikely to be the right thing to do for an object managed by a smart pointer.
If deleting via a pointer to incomplete type had always had Undefined Behavior, then the standard could just require the compiler to diagnose it and fail. But it's well-defined when the real destructor is trivial: knowledge that the programmer can have, but the compiler doesn't have. Why the language defines and allows this is beyond me, but C++ supports many practices that today are not regarded as best practices.
A complete type has a known size, and hence, sizeof(C)
will compile if and only if C
is a complete type -- with known destructor. So it can be used as a guard. One way would be simply
(void) sizeof(C); // Type must be complete
I would guess that with some compiler and options the compiler optimizes it away before it could notice that it shouldn't compile, and that the enum
is a way to avoid such non-conforming compiler behavior:
enum { type_must_be_complete = sizeof(C) };
An alternative explanation for the choice of enum
rather than just a discarded expression, is simply personal preference.
Or as James T. Hugget suggests in a comment to this answer, “The enum may be a way of creating a pseudo-portable error message at compile time”.
(1) The default-generated do-nothing destructor for an incomplete type was a problem with old std::auto_ptr
. It was so insidious that it made its way into a GOTW item about the PIMPL idiom, written by the chair of the international C++ standardization committee Herb Sutter. Of course, nowadays that std::auto_ptr
is deprecated, one will instead use some other mechanism.
ptr_
itself in thesizeof
assizeof(*ptr_)
instead ofsizeof(C)
.