[英]How to explicitly call a conversion function whose conversion-type-id contains a placeholder specifier
struct A{
operator auto(){
return 0;
}
};
int main(){
A a;
a.operator auto(); // #1
a.operator int(); // #2
}
GCC accepts that #2 is the right way to call the conversion function explicitly while Clang accepts #1. GCC接受 #2 是明确调用转换 function 的正确方法,而Clang接受 #1。
It seems that #1
is ill-formed due to the following rule:由于以下规则, #1
似乎格式错误:
dcl.spec.auto#6 dcl.spec.auto#6
A program that uses auto or decltype(auto) in a context not explicitly allowed in this section is ill-formed.在本节未明确允许的上下文中使用 auto 或 decltype(auto) 的程序是格式错误的。
This usage a.operator auto()
is not explicitly allowed in section [dcl.spec.auto], hence it should be ill-formed.这种用法a.operator auto()
在 [dcl.spec.auto] 部分中没有明确允许,因此它应该是格式错误的。 However, for the second usage, which is accepted by GCC, the standard does not say that the conversion-function-id
where the conversion-type-id
is replaced by the deduced type denotes the name of the conversion function.但是,对于 GCC 接受的第二种用法,标准并没有说conversion-type-id
被推导类型替换的conversion-function-id
表示转换 function 的名称。 In other words, the declared conversion-function-id
in the declaration is operator auto
rather than operator int
.换句话说,声明中声明的conversion-function-id
是operator auto
而不是operator int
。 The former has the same token as the declarator-id of the declaration.前者与声明的 declarator-id 具有相同的标记。 According to the grammar, the unqualified-id operator auto
should be the name of that conversion function.根据语法,unqualified-id operator auto
应该是该转换的名称 function。 So, how to explicitly call this conversion function?那么,如何显式调用这个转换function呢? Is it underspecified in the standard about which is the name of the conversion function when it contains a placeholder specifier?当转换 function 包含占位符说明符时,它是否在标准中未指定?
It seems, that this is not specified precisely enough.似乎,这没有足够精确地指定。
10.1.7.4 The auto specifier
:从10.1.7.4 The auto specifier
:The placeholder type can appear with a function declarator in the decl-specifier-seq, type-specifier-seq, conversion-function-id , or trailing-return-type, in any context where such a declarator is valid.在这种声明符有效的任何上下文中,占位符类型可以在 decl-specifier-seq、type-specifier-seq、 conversion-function-id或 trailing-return-type 中与 function 声明符一起出现。
Reading precisely, one might distinguish here between "can" and the stronger "can only", ie potentially opening up room for degrees of freedom for compiler intrinsics (strictly wrong vs. unspecified behavior).仔细阅读,人们可能会在这里区分“可以”和更强的“只能”,即可能为编译器内在函数的自由度打开空间(严格错误与未指定的行为)。
And 3.4.5 class member access
says:和3.4.5 class member access
说:
7 If the id-expression is a conversion-function-id, its conversion-type-id is first looked up in the class of the object expression and the name, if found, is used. 7 如果 id-expression 是转换函数 ID,则首先在 object 表达式的 class 中查找其转换类型 ID,如果找到,则使用名称。
Again leaving room for interpretation if the auto keyword can effectively be a fully qualified conversion-type-id within this context or not.如果 auto 关键字在此上下文中可以有效地成为完全限定的转换类型 ID,则再次留下解释空间。
Your question itself might have to be further branched, namely您的问题本身可能必须进一步分支,即
I've seen explicit tests for this within several clang revisions so its behavior is not an artefact of implicit naming convention applicance but an explicitly desired behavior obviously.我已经在几个 clang 修订版中看到了对此的明确测试,因此它的行为不是隐式命名约定应用的人工制品,而是明显需要的行为。
As already mentioned within the comments, Clang's behavior is a bit more overall consistent here at least in comparison to gcc since it's totally clear there, where the auto keyword is used for type deduction and where for name / function-id resolution.正如评论中已经提到的那样,至少与 gcc 相比,Clang 的行为在这里更加一致,因为它在那里完全清楚,其中 auto 关键字用于类型推断以及名称/函数 ID 解析的位置。 The operator auto() there is handled as a more explicit own entity, whereas for gcc, it has anonymous character similar to a lambda but is involved within candidates competition even for the explicit member operator access way.这里的操作符 auto() 被作为一个更明确的自己的实体处理,而对于 gcc,它具有类似于 lambda 的匿名字符,但即使对于显式成员操作符访问方式也参与了候选人竞争。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.