Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't ~ followed by :: parse

Tags:

c++

c++11

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

like image 850
mark Avatar asked Jan 04 '13 11:01

mark


1 Answers

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.

like image 95
n. 1.8e9-where's-my-share m. Avatar answered Sep 21 '22 11:09

n. 1.8e9-where's-my-share m.