为什么 C++不允许用户定义运算符?

发布于 2024-10-08 20:49:42 字数 399 浏览 6 评论 0原文

我想知道这个问题已经有一段时间了。它们已经有一大堆并且可能会超载,那么为什么不进行到底并允许自定义运算符呢?我认为这可能是一个很好的补充。

有人告诉我,这会使该语言变得难以编译。这让我想知道,C++ 无论如何都不能真正被设计为易于编译,那么它真的是不可撤销的吗?当然,如果你使用带有静态表和语法的 LR 解析器,那么

E → T + E | T
T → F * T | F
F → id | '(' E ')'

它就不起作用了。在 Prolog 中,通常使用运算符优先级解析器 AFAIK 进行解析,可以轻松定义新运算符,但该语言要简单得多。现在,显然可以重写语法,以便在将运算符硬编码到语法中的每个位置都接受标识符。

还有哪些其他解决方案和解析器方案以及哪些其他因素影响了设计决策?

I've been wondering this for quite some time. There are already a whole bunch of them and they can be overloaded, so why not do it to the end and allow custom operators? I think it could be a great addition.

I've been told that this would make the language too hard to compile. This makes me wonder, C++ cannot really be designed for easy compilation anyway, so is it really undoable? Of course, if you use an LR parser with a static table and a grammar such as

E → T + E | T
T → F * T | F
F → id | '(' E ')'

it wouldn't work. In Prolog, which is usually parsed with a Operator-Precedence parser AFAIK, new operators can easily be defined, but the language is much simpler. Now, the grammar could obviously be rewritten to accept identifiers in every place where an operator is hard-coded into the grammar.

What other solutions and parser schemes are there and what other things have influenced that design decision?

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

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

发布评论

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

评论(7

半世晨晓 2024-10-15 20:49:42

http://www2.research.att.com/~bs/bs_faq2 .html#overload-operator

这种可能性已被考虑过多次,但每次我/我们都认为可能的问题超过了可能的好处。

这不是语言技术问题。甚至当我在 1983 年第一次考虑它时,我就知道如何实施它。然而,我的经验是,当我们超越最琐碎的例子时,人们似乎对运算符使用的“明显”含义有微妙的不同看法。一个经典的例子是a**b**c。假设 ** 表示求幂。现在 a**b**c 应该表示 (a**b)**ca**(b**c) ?我认为答案是显而易见的,我的朋友们也同意了——然后我们发现我们并没有就哪一个解决方案是显而易见的达成一致。我的猜测是,此类问题会导致微妙的错误。

http://www2.research.att.com/~bs/bs_faq2.html#overload-operator

The possibility has been considered several times, but each time I/we decided that the likely problems outweighed the likely benefits.

It's not a language-technical problem. Even when I first considerd it in 1983, I knew how it could be implemented. However, my experience has been that when we go beyond the most trivial examples people seem to have subtlely different opinions of "the obvious" meaning of uses of an operator. A classical example is a**b**c. Assume that ** has been made to mean exponentiation. Now should a**b**c mean (a**b)**c or a**(b**c)? I thought the answer was obvious and my friends agreed - and then we found that we didn't agree on which resolution was the obvious one. My conjecture is that such problems would lead to subtle bugs.

丘比特射中我 2024-10-15 20:49:42

编译起来会比现在更加困难。另外,运算符的优先级也会存在问题:如何定义它?您需要一种方法来告诉编译器用户定义的运算符优先于另一个运算符。

几乎可以肯定这是可行的,但我认为 C++ 不需要其他方式搬起石头砸自己的脚:-)

It would become even harder to compile than what already is. Also, there would be problems with operators' precedence: how do you define it? You need a way to tell the compiler that an user-defined operator has precedence over another operator.

Almost surely it's feasible, but I think that C++ doesn't need other ways to shoot yourself in the foot :-)

不美如何 2024-10-15 20:49:42

这将使语言变得更加复杂。这显然是不可取的。

不过,请查看Boost Spirit。使用大量模板元编程技巧来使像您提到的那样的东西成为可能还有很长的路要走。

This would make the language even more complex. And that obviously wouldn't be desirable.

Still, check out Boost Spirit. It goes a long way to make stuff like you mentioned possible using lots of template metaprogramming tricks.

那伤。 2024-10-15 20:49:42

实际上它的设计目的是非常容易解析和编译。 C 有 32 个定义的关键字,所有其他标记都是函数和变量。

C++ 只剩下一些了。人们可以轻松识别哪个标记代表哪个标记,因此知道当使用 + 标记或其他标记时要查找什么。

Actually it's designed to be very easy to parse and compile. C has 32 defined keywords, all other tokens are function and variables.

C++ only has a few more. One can easily identify which token is for which, so know what to look for when one uses the + token or whatever.

浪菊怪哟 2024-10-15 20:49:42

允许自定义运算符的问题在于,您还必须允许程序员指定如何使用运算符的语法。我认为 C++ 类型系统可以提供一点帮助,但它将有助于解决诸如关联性等问题。

这将使本已复杂的语言变得更加复杂......

The problem with allowing custom operators is that you also have to allow the programmer to specify the syntax for how the operators should be used. I suppose the C++ type system could help a little, but it would help resolving issues like associativity, etc.

It would make the already complex language, much more complex...

缺⑴份安定 2024-10-15 20:49:42

这通常是可以避免的,因为大多数代码是由多个人编写的,因此代码应该是“可审查的”,并且它几乎不是一种语言的“所需”功能。

Joel Spolsky 有一篇关于此的好文章

This is usually avoided because most code is written by more that one man, so the code should be "reviewable", and it's hardly a "desired" feature of a language.

Joel Spolsky have a good article about this.

情感失落者 2024-10-15 20:49:42

我刚刚发现实际上可以实现与重载运算符非常相似的东西。考虑

Vector v, a, b; v = a /vectorProduct/ b;

事实证明,您可以通过使用由现有运算符分隔的虚拟类来实现自定义运算符的行为。 =)

I just found out that it's actually possible to achieve something very similar to overloaded operators. Consider

Vector v, a, b; v = a /vectorProduct/ b;

Turns out you can achieve the behaviour of custom operator by using dummy classes delimited by existing operators. =)

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