Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicit destructor call with decltype

Consider the following snippet:

struct Foo {};

int main()
{
   Foo f;
   f.~decltype(f)(); // fine with clang, error with gcc
   f.~decltype(auto)(); // error with both clang and gcc 
}

The rules for an explicit destructor call are handled by the standard grammar with pseudo-destructor-name which is defined as follows:

pseudo-destructor-name:
nested-name-specifier opt type-name :: ~ type-name
nested-name-specifier template simple-template-id :: ~type-name
~ type-name
~ decltype-specifier

And:

decltype-specifier:
decltype ( expression )
decltype ( auto )

Then shouldn't the above snippet be well-formed as per standard? (Not considering the fact that the destructor is called twice and then a third time on the same object.)

GCC Live
Clang Live

like image 323
DeiDei Avatar asked Oct 24 '17 12:10

DeiDei


1 Answers

Your program is ill-formed.
§7.1.6.4/[dcl.spec.auto] states:

A program that uses auto or decltype(auto) in a context not explicitly allowed in this section is ill-formed.

There, I cannot find anything that should allow you to write this. Generally, decltype(auto) is used in variable and function declarations only. The fact the grammar allows is doesn't mean it's well-formed, though.

Therefore, writing something like f.~decltype(f)() hasn't been explicitely forbidden and is allowed as stated in the grammar. The fact that the GCC won't compile it is most likely a bug.

like image 198
Jodocus Avatar answered Oct 03 '22 18:10

Jodocus