During Andrei Alexandrescu's talk on error handling:
See C++ and Beyond 2012: Andrei Alexandrescu - Systematic Error Handling in C++ (about 30 minutes in)
Andrei presents the following piece of code:
~Expected()
{
using std::exception_ptr;
if (gotHam) ham.~T();
else spam.~exception_ptr();
}
This destructor is cleaning up a union
which contains either some type T
or a std::exception_ptr
. The union is populated using placement new
.
Andrei then explains that the using std::exception_ptr;
is necessary because the following code does not parse:
else spam.~std::exception_ptr();
This means that it is always necessary to have a using directive if you need to explicitly call the destructor of a class in a different namespace.
Why doesn't the second example parse?
Would the followng code be a valid alternative?
else delete spam;
Does this have the same affect as explicitly calling the destructor of std::exception_ptr
Andrei probably uses using std::exception_ptr;
because his compiler is broken.
There's no need. spam.~exception_ptr();
should compile just fine without it.
3.4.5/3. If the unqualified-id is ~type-name, the type-name is looked up in the context of the entire postfix-expression. If the type T of the object expression is of a class type C, the type-name is also looked up in the scope of class C.
It indeed compiles with gcc.
If you need to use qualified-name for some reason, spam.std::exception_ptr::~exception_ptr();
also compiles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With