Created on 2000-08-17.00:00:00 last changed 171 months ago
Rationale:
The standard is clear as written; this is a request for change, not a defect in the strict sense. The LWG had several different objections to the proposed change. One is that it would prevent users from creating objects of type unary_function and binary_function. Doing so can sometimes be legitimate, if users want to pass temporaries as traits or tag types in generic code.
Proposed resolution:
When a base class in the standard library is useful only as an interface specifier, i.e., when an object of the class will never be directly instantiated, specify that the class contains a protected destructor. This will prevent deletion through a pointer to the base class without performance, or space penalties (on any implementation I'm aware of).
As an example, replace...
template <class Arg, class Result> struct unary_function { typedef Arg argument_type; typedef Result result_type; };
... with...
template <class Arg, class Result> struct unary_function { typedef Arg argument_type; typedef Result result_type; protected: ~unary_function() {} };
Affected definitions:
20.3.1 [lib.function.objects] -- unary_function, binary_function
24.3.2 [lib.iterator.basic] -- iterator
According to the November 1997 Draft Standard, the results of deleting an object of a derived class through a pointer to an object of its base class are undefined if the base class has a non-virtual destructor. Therefore, it is potentially dangerous to publicly inherit from such base classes.
Defect:
The STL design encourages users to publicly inherit from a number of classes
which do nothing but specify interfaces, and which contain non-virtual
destructors.
Attribution:
Wil Evers and William E. Kempf suggested this modification for functional
objects.
History | |||
---|---|---|---|
Date | User | Action | Args |
2010-10-21 18:28:33 | admin | set | messages: + msg2030 |
2010-10-21 18:28:33 | admin | set | messages: + msg2029 |
2000-08-17 00:00:00 | admin | create |