使用decltype的显式析构函数调用 [英] Explicit destructor call with decltype
问题描述
请考虑以下代码段:
struct Foo {};
int main()
{
Foo f;
f.~decltype(f)(); // fine with clang, error with gcc
f.~decltype(auto)(); // error with both clang and gcc
}
显式析构函数调用的规则由标准语法pseudo-destructor-name
处理,其定义如下:
The rules for an explicit destructor call are handled by the standard grammar with pseudo-destructor-name
which is defined as follows:
伪析构函数名称:
嵌套名称说明符opt类型名称::〜类型名称
嵌套名称说明符模板simple-template-id ::〜类型名称
〜类型名称
〜decltype-specifier
pseudo-destructor-name:
nested-name-specifier opt type-name :: ~ type-name
nested-name-specifier template simple-template-id :: ~type-name
~ type-name
~ decltype-specifier
并且:
decltype-specifier:
decltype(表达式)
decltype(auto)
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.)
推荐答案
您的程序格式不正确.
§7.1.6.4/[dcl.spec.auto] 指出:
Your program is ill-formed.
§7.1.6.4/[dcl.spec.auto] states:
在本节未明确允许的上下文中使用
auto
或decltype(auto)
的程序格式错误.
A program that uses
auto
ordecltype(auto)
in a context not explicitly allowed in this section is ill-formed.
在这里,我找不到任何应该允许您编写的内容.通常,decltype(auto)
仅用于变量和函数声明.语法允许的事实并不意味着它的格式正确.
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.
因此,没有明确禁止编写类似f.~decltype(f)()
的内容,并且可以按照语法中的说明进行编写. GCC无法编译的事实很可能是一个错误.
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.
这篇关于使用decltype的显式析构函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!