Re: Explicit destructor call - problem with basic types and types
from other namespaces
Am 09.05.2012 21:26, schrieb PiotrN:
Two questions:
1) Why I cannot call destructor for builtin types like int:
int a;
int* p = new (&a) int(7);
p->~int();
I found in the NET that standard says: "The notation for explicit call
of a destructor can be used for any scalar type name. Allowing this
makes it possible to write code without having to know if a destructor
exists for a given type. "
Yes, this is roughly the rationale for it: To be able to write generic
code without much trickery to discriminate scalar types from class
types. The more precise rule is that the pseudo-destructor name is valid
for scalar types only, if these are provided as /type-name/ (like a
typedef) or a decltype-specifier (like: decltype(0)).
And typedef int is "scalar type name" but int is not - so it works the
following:
typedef int INT;
p->~INT();
Why standard does allow calling destructor on typedef (and template
typename) - but it has objections to plain type? Is there any
rationale for this?
Only a weak one: Once you are in non-template code, you have knowledge
of the specific type, so why bothering with writing a no-op? This *is*
really a weak argument, because you have the same problem once you would
try to write a generic macro. In other words: I wish, we would not have
this constraint to support the pseudo-destructor only for type-names and
decltype-specifiers when we have a non-class type.
2) Why C++ (gcc-4.3.4) has objections to this syntax:
p->std::~string();
of course this works well:
using namespace std;
p->~string();
You can write this as:
p->std::string::~string();
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]