转换函数的显式调用和隐式调用有什么区别?

发布于 2025-01-19 21:50:32 字数 1033 浏览 0 评论 0原文

考虑这个例子:

struct A{
    template<class T>
    operator T();  // #1
};
struct B:A{
    template<class U>
    operator U&&();  // #2
};
int main(){
  B  b;
  int a = b;  // #3
  b.operator int();  // #4
}

根据 [class.member.lookup] p7

如果 N 是非依赖转换函数 ID,则考虑属于 T 成员的转换函数模板。对于每个这样的模板 F,查找集 构造 S(t,T) 时,仅当函数模板声明对应于 F 的声明时才将其视为名称为 t ([basic.scope.scope] )。每个此类查找集的声明集的成员(不得是无效集)都包含在结果中。

无论 #3 中的 conversion-function-id 是什么,#1#2 都包含在查找结果中#4#3诊断正是我们所期望的,换句话说,两者#1#2 是候选者,它们是无法区分的。

然而,似乎 实现 仅在以下情况下将 #2 视为唯一候选者:处理#4。如上所述,#3#4 的候选集应该相同。我是否遗漏了一些导致差异的其他规则?或者,这是实现中的错误吗?

Consider this example:

struct A{
    template<class T>
    operator T();  // #1
};
struct B:A{
    template<class U>
    operator U&&();  // #2
};
int main(){
  B  b;
  int a = b;  // #3
  b.operator int();  // #4
}

According to [class.member.lookup] p7

If N is a non-dependent conversion-function-id, conversion function templates that are members of T are considered. For each such template F, the lookup set
S(t,T) is constructed, considering a function template declaration to have the name t only if it corresponds to a declaration of F ([basic.scope.scope]). The members of the declaration set of each such lookup set, which shall not be an invalid set, are included in the result.

#1 and #2 are both included in the lookup result regardless of what the conversion-function-ids are in #3 and #4. The diagnosis for #3 is what we expect, in other words, both #1 and #2 are candidates and they are indistinguishable.

However, it seems that implementations only consider #2 as the unique candidate when processing #4. As said above, the candidate set should be the same for either #3 or #4. Do I omit some other rules that cause the difference? Or, Is it a bug in implementations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2025-01-26 21:50:32

实现只是还没有跟上新的(澄清的)规则,这些规则必须在 2020 年很大程度上发明,因为没有发布的标准版本以任何合理的方式描述了转换函数模板的查找。

Implementations just haven’t caught up to the new(ly clarified) rules, which had to be largely invented in 2020 since no published standard version has ever described lookup for conversion function templates in any sensible way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文