By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Is explicit template qualification required for explicit delete?

P: n/a
I've been working on the configuration infrastructure for the ACE
framework which contains code that uses a templatized handle/body
idiom with placement new/delete. With g++ 3.4, we found that the
delete needed the addition of a template argument that wasn't needed
in earlier versions. I've checked this with Comeau C++ Online in
strict mode, and with Gimpel's Flexelint and neither reported any
issues with the code without the template argument. I realize that
whether a given compiler accepts/rejects a bit of code doesn't mean
anything, so I've checked C++PLv3 and the C++ standard, but haven't
been able to tell whether g++ is being strict, or is in error.

Here is some sample code that demonstrates the issue:

#include <cstdlib>
#include <new>

template <class T>
class Body
{
public:
Body () { }
~Body () { }
};

template <class T>
class Handle
{
public:
Handle ()
{
void *ptr = std::malloc(sizeof(Body<T>));
x_ = new (ptr) Body<T> ();
}

virtual ~Handle ()
{
#ifdef WITH_TEMPLATE_ARG
x_->~Body <T> ();
#else
x_->~Body ();
#endif
std::free(x_);
}

private:
Body<T> *x_;
};
int
main()
{
Handle<int> *x = new Handle<int> ();
delete x;

return 0;
}

Everything I've tried compiles this code with WITH_TEMPLATE_ARG defined,
but so far only g++ 3.4 rejects it without.

Many thanks,

--jtc

--
J.T. Conklin
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"J.T. Conklin" <jt*@acorntoolworks.com> wrote in message
news:87************@orac.acorntoolworks.com...
I've been working on the configuration infrastructure for the ACE
framework which contains code that uses a templatized handle/body
idiom with placement new/delete. With g++ 3.4, we found that the
delete needed the addition of a template argument that wasn't needed
in earlier versions. I've checked this with Comeau C++ Online in
strict mode, and with Gimpel's Flexelint and neither reported any
issues with the code without the template argument. I realize that
whether a given compiler accepts/rejects a bit of code doesn't mean
anything, so I've checked C++PLv3 and the C++ standard, but haven't
been able to tell whether g++ is being strict, or is in error.

Here is some sample code that demonstrates the issue:

#include <cstdlib>
#include <new>

template <class T>
class Body
{
public:
Body () { }
~Body () { }
};

template <class T>
class Handle
{
public:
Handle ()
{
void *ptr = std::malloc(sizeof(Body<T>));
x_ = new (ptr) Body<T> ();
}

virtual ~Handle ()
{
#ifdef WITH_TEMPLATE_ARG
x_->~Body <T> ();
#else
x_->~Body ();
#endif
std::free(x_);
}

private:
Body<T> *x_;
};
int
main()
{
Handle<int> *x = new Handle<int> ();
delete x;

return 0;
}

Everything I've tried compiles this code with WITH_TEMPLATE_ARG defined,
but so far only g++ 3.4 rejects it without.


Well, my copy of the standard says that a destructor call is ~class-name,
and a template-id (template name with arguments, e.g. Handle<int>) qualifies
as a class-name, so the universal acceptance of ~Body<T>() makes sense to
me. The other option for class-name is identifier, which doesn't help me
much. It is true that Handle is an identifier, but that doesn't necessarily
mean that it can be used to invoke the destructor, right? I'm probably
missing something. You might want to also post this to
comp.lang.c++.moderated, if you haven't already.

--
David Hilsee
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.