为什么 C++不允许用户定义运算符?
我想知道这个问题已经有一段时间了。它们已经有一大堆并且可能会超载,那么为什么不进行到底并允许自定义运算符呢?我认为这可能是一个很好的补充。
有人告诉我,这会使该语言变得难以编译。这让我想知道,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
http://www2.research.att.com/~bs/bs_faq2 .html#overload-operator
http://www2.research.att.com/~bs/bs_faq2.html#overload-operator
编译起来会比现在更加困难。另外,运算符的优先级也会存在问题:如何定义它?您需要一种方法来告诉编译器用户定义的运算符优先于另一个运算符。
几乎可以肯定这是可行的,但我认为 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 :-)
这将使语言变得更加复杂。这显然是不可取的。
不过,请查看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.
实际上它的设计目的是非常容易解析和编译。 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.
允许自定义运算符的问题在于,您还必须允许程序员指定如何使用运算符的语法。我认为 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...
这通常是可以避免的,因为大多数代码是由多个人编写的,因此代码应该是“可审查的”,并且它几乎不是一种语言的“所需”功能。
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.
我刚刚发现实际上可以实现与重载运算符非常相似的东西。考虑
事实证明,您可以通过使用由现有运算符分隔的虚拟类来实现自定义运算符的行为。 =)
I just found out that it's actually possible to achieve something very similar to overloaded operators. Consider
Turns out you can achieve the behaviour of custom operator by using dummy classes delimited by existing operators. =)