简体   繁体   English

如何显式调用转换类型 ID 包含占位符说明符的转换 function

[英]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-idoperator 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.似乎,这没有足够精确地指定。

  1. From 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您的问题本身可能必须进一步分支,即

  1. What are the overloading rules for the operator auto() usage in detail, ie should it be available for regular candidates competition already on class definition level?运算符 auto() 使用的重载规则是什么,即它是否应该可用于已经在 class 定义级别上的常规候选人竞赛? (not the case for Clang and Gcc, both accept the operator a priori besides an extra operator int()...) (不是 Clang 和 Gcc 的情况,除了额外的运算符 int() 之外,它们都先验地接受运算符...)
  2. Can the operator auto() be called with explicit member operator referring (your case 1), ie effectively, has it a (unique) accessible name?可以使用显式成员运算符引用(您的情况 1)调用运算符 auto(),即有效地,它是否具有(唯一)可访问名称? Allowing that would be contradictory to all other explicitly allowed use cases for the keyword.允许这将与关键字的所有其他明确允许的用例相矛盾。

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.

相关问题 我想看一个例子,其中在整个postfix-expression的上下文中查找conversion-type-id - I'd like to see one example where the conversion-type-id is looked up in the context of the entire postfix-expression 通过转换运算符调用显式实例化的模板函数 - Call of explicitly instantiated template function through conversion operator Visual C ++ - 显式调用基本类型的转换运算符 - Visual C++ - call conversion operator on a primitive type explicitly Boost Spirit占位符类型转换 - Boost Spirit placeholder type conversion 自动类型转换功能调用C ++ - Automatic type conversion function call C++ 如何在C ++中使用转换说明符? - How to use conversion specifier character in C++? 具有简单类型说明符的显式类型转换(功能表示法) - Explicit type conversion (functional notation) with simple-type-specifier 双重自动转换功能定义,gcc错误:错误:预期的类型说明符 - Double Automatic Conversion Function definition, gcc error: error: expected type-specifier 为什么转换 function 声明不需要至少一个定义类型说明符 - why a conversion function declaration does not require at least one defining-type-specifier 将显式实例化的函数模板与转换匹配 - Matching explicitly instantiated function template with conversion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM