运算符重载-为什么要静态解析? [英] Operator overloading - why static resolve?

查看:96
本文介绍了运算符重载-为什么要静态解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

静态解决重载运算符的原因是什么?对我来说,这似乎是一个奇怪的选择-我能想到的唯一优点是性能有所提高(但是JIT有时也可以避免这种情况),但付出的代价是一些不太直观的行为-即,我基本上必须将操作员转发给一个虚拟函数来获得想要的行为.

What are the reasons for resolving overloaded operators statically? It seems a strange choice to me - the only advantage I can think of is a small performance gain (but then the JIT may avoid that sometimes as well) at the cost of some rather unintuitive behavior - i.e. I basically have to forward the operator to a virtual function to get the wanted behavior.

这是刚从C ++接管的吗,还是有其他一些很好的理由呢?

Was this just taken over from C++ or are there some other good reasons for this?

推荐答案

请参阅Eric Lipperts文章

See Eric Lipperts article Why are overloaded operators always static in C#?

当面对潜在的语言功能时,我们应该问自己的问题是:该功能的引人注目的好处是否证明所有费用合理?"而且,成本远远不只是设计,开发,测试,记录和维护功能所需的平凡的成本.会有更多微妙的成本,例如,此功能将来会使更改类型推断算法变得更加困难吗?这是否会导致我们进入一个不引入向后兼容性中断就无法进行更改的世界?依此类推.

Rather, the question we should be asking ourselves when faced with a potential language feature is "does the compelling benefit of the feature justify all the costs?" And costs are considerably more than just the mundane dollar costs of designing, developing, testing, documenting and maintaining a feature. There are more subtle costs, like, will this feature make it more difficult to change the type inferencing algorithm in the future? Does this lead us into a world where we will be unable to make changes without introducing backwards compatibility breaks? And so on.

在这种情况下,引人注目的收益很小.如果您想在C#中使用虚拟的调度重载运算符,则可以非常轻松地从静态部分中构建一个. [...]

可以支持基于实例的运算符,但是与使它正确工作所需的努力相比,C#语言设计人员没有看到太大的收获.

It is be possible to support instance based operators but the C# language designer did not see the big gain compared to the efforts needed to make it work correctly.

这篇关于运算符重载-为什么要静态解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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