Re: When is a destructor "used"?
On Mar 26, 8:20 am, "Andrew J. Bromage" <degue...@gmail.com> wrote:
Quick question, with a possibly non-quick answer.
Consider this snippet:
template<typename T>
class Container
{
public:
// NOTE: This is throw-none.
Container(T* p_obj = 0) throw() : m_obj(p_obj) {}
~Container()
{
// Use checked_delete() to avoid UB.
boost::checked_delete(m_obj);
}
private:
T* m_obj;
};
class Foo;
struct Bar
{
Container<Foo> foo1;
Container<Foo> foo2;
Bar() {} // XXX
~Bar();
}
The question is: Is the XXX line correct?
Certainly. There may be other problems, however.
The question relates to whether or not the destructor for the
Containers is "used" in this function.
Some compilers reason that
the constructor of foo2 might fail, and so the destructor for foo1
is "used". But at least one (Forte) notices that this can't happen
because the constructor of foo2 is declared as throw-none.
And boost::checked_delete requires a complete type if it is
instantiated, right? So if Container::~Container is
instantiated, the program fails. (Otherwise, I can't see where
there would be any problem.)
Who is right?
I don't think that the standard actually addresses this issue.
Obviously, the destructor must be instantiated at some point,
since it will be called by the destructor of Bar. The real
question is: if a compiler can determine that a given function
or block of code cannot throw, is it permitted to not require a
definition of code that would only be executed if it did throw?
Given the rest of the standard, I don't think that there was
ever any intent to require static analysis of such code, so
"using" the destructor, or considering it used, is almost
certainly legal. I'm not sure that this is required, however;
as the standard currently stands, I'd say that this is
unspecified. Which means that both behaviors are acceptable.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]