转换函数的显式调用和隐式调用有什么区别?
考虑这个例子:
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 namet
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现只是还没有跟上新的(澄清的)规则,这些规则必须在 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.