Explicit destructor call with decltype

回眸只為那壹抹淺笑 提交于 2019-12-22 03:15:26

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/46910844/explicit-destructor-call-with-decltype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!