为什么不缩小影响重载分辨率? [英] Why doesn't narrowing affect overload resolution?

查看:117
本文介绍了为什么不缩小影响重载分辨率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下内容:

struct A {
    A(float ) { }
    A(int ) { }
};

int main() {
    A{1.1}; // error: ambiguous
}

这无法编译有关模糊过载的错误 A :: A 。两个候选人都被认为是可行的,因为要求是简单的:

This fails to compile with an error about an ambiguous overload of A::A. Both candidates are considered viable, because the requirement is simply:


其次,对于 F 为一个可行的函数,每个参数都应存在一个隐式转换序列(13.3.3.1),该转换序列将该参数转换为 F 的相应参数。

Second, for F to be a viable function, there shall exist for each argument an implicit conversion sequence (13.3.3.1) that converts that argument to the corresponding parameter of F.

虽然有一个从 double int A(int)重载不是实际上可行的(在规范的非C ++标准意义上) - 这将涉及缩小的转换,从而不合格。

While there is an implicit conversion sequence from double to int, the A(int ) overload isn't actually viable (in the canonical, non-C++-standard sense) - that would involve a narrowing conversion and thus be ill-formed.

为什么缩小在确定可行候选人的过程中不考虑的转化?有没有其他情况下,过载被认为是模糊的,尽管只有一个候选人是可行的?

Why are narrowing conversions not considered in the process of determining viable candidates? Are there any other situations where an overload is considered ambiguous despite only one candidate being viable?

推荐答案

问题在于可以根据类型检测到缩小的转化次数。

A problem lies with the fact that narrowing conversions can be detected not based on types.

在C ++中在编译时生成值非常复杂。

There are very complex ways to generate values at compile time in C++.

阻塞变窄是一件好事。

Blocking narrowing conversions is a good thing. Making the overload resolution of C++ even more complex than it already is is a bad thing.

在确定过载分辨率时忽略变换规则(这使得重载分辨率纯粹关于类型) ,然后在所选择的过载导致变窄的转换时错误出错,使过载分辨率变得更加复杂,并且增加了检测和防止变窄转换的方式。

Ignoring narrowing conversion rules when determining overload resolution (which makes overload resolution purely about types), and then erroring out when the selected overload results in a narrowing conversion, keeps overload resolution from being even more complex, and adds in a way to detect and prevent narrowing conversions.

只有一个候选者可行的两个示例是在实例化和复制列表初始化(其中考虑显式构造函数)时失败的模板函数,但如果它们选择,你会得到一个错误)。类似地,具有冲击超载分辨率将使得超载分辨率甚至比它已经是更复杂。

Two examples where only one candidate is viable would be template functions that fail "late", during instantiation, and copy-list initialization (where explicit constructors are considered, but if they are chosen, you get an error). Similarly, having that impact overload resolution would make overload resolution even more complex than it already is.

现在,人们可能会问,为什么不将折叠缩小纯粹类型系统?

Now, one might ask, why not fold narrowing conversion purely into the type system?

缩小转换纯粹基于类型是不可行的。这样的更改可能会破坏编译器可以证明有效的大量遗留代码。

Making narrowing conversion be purely type-based would be non-viable. Such changes could break huge amounts of "legacy" code that the compiler could prove as being valid. The effort required to sweep a code base is far more worthwhile when most of the errors are actual errors, and not the new compiler version being a jerk.

unsigned char buff[]={0xff, 0x00, 0x1f};

这会在基于类型的缩小转换下失败,因为 0xff 的类型 int ,这样的代码很常见。

this would fail under a type-based narrowing conversion, as 0xff is of type int, and such code is very common.

修改 int 字面量到 unsigned char 字面值,赔率是扫描将结束与我们设置一个标志告诉编译器关闭了愚蠢的错误。

Had such code required pointless modification of the int literals to unsigned char literals, odds are the sweep would have ended with us setting a flag to tell the compiler to shut up about the stupid error.

这篇关于为什么不缩小影响重载分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆