简体   繁体   English

为什么不是=> C ++中的可重载运算符?

[英]Why isn't => an overloadable operator in C++?

=> isn't overloadable in C++. =>在C ++中不可重载。 I have always wondered whether this is just a pure coincidence or whether there is a specific reason this sequence of two characters cannot be overloaded. 我一直想知道这是纯粹的巧合,还是有一个特定的原因不能导致两个字符的序列重载。 The emergence of the comparison operator <=> leads me to believe the former. 比较运算符<=>的出现使我相信前者。

I think it's a prime candidate for overloading because it looks like an arrow, which is very common in functional programming syntax and mathematics. 我认为它是重载的最佳选择,因为它看起来像箭头,这在函数式编程语法和数学中非常常见。 Granted -> exists already but having another arrow could be useful to disambiguate between the two. 授予->已经存在,但使用另一个箭头可能有助于消除两者之间的歧义。 I also don't see how it could break backwards compatibility. 我也看不到它如何破坏向后兼容性。

C++ only allows you to overload operators that are already operators in the base language. C ++仅允许您重载已经是基本语言运算符的运算符。

The base language defined operators, each with its associated precedence and associativity. 基本语言定义了运算符,每个运算符都有其关联的优先级和关联性。 In an operator overload, you can supply the meaning of that operator (the code that will be executed) for types for a user-defined type (or sometimes, for a combination of two user-defined types). 在运算符重载中,可以为用户定义类型的类型(或有时为两个用户定义类型的组合)提供该运算符的含义 (将执行的代码)。

You cannot, however, just choose an arbitrary set of symbols, and treat it as an operator (no matter how attractive that might be). 但是,您不能只选择任意符号集并将其视为运算符(无论它有多诱人)。

There are languages that do allow this--for example, ML (and many of its descendants) allow you to define an operator with an entirely new name that's not part of the base language at all. 有些语言确实允许这样 -例如,ML(及其许多后代)允许您使用一个完全不属于基础语言一部分的全新名称来定义运算符。 When you do this, you define the precedence and associativity you want that operator to have. 在执行此操作时,可以定义该运算符具有的优先级和关联性。 I think that's great, and provides a useful capability--but (at least as the capability is defined in ML) it also has some weaknesses that probably wouldn't fit nearly so well with how things work in C++. 我认为这很棒,并且提供了有用的功能-但是(至少是因为该功能是在ML中定义的),它也存在一些弱点,可能与C ++的工作方式不太相称。 I wouldn't expect to see it as part of C++ any time soon (or, probably, ever). 我不希望很快(或者可能永远)将其视为C ++的一部分。

In C++, “overloading” is a mechanism for providing a custom definition for existing operators so they can be used in custom types, not a mechanism for adding new operators to the language. 在C ++中,“重载”是一种为现有运算符提供自定义定义的机制,以便可以在自定义类型中使用它们,而不是向该语言添加新运算符的机制。 You can't change the meaning of a “=>” operator because C++ (as of this writing at least) doesn't have a “=>” operator. 您无法更改“ =>”运算符的含义,因为C ++(至少在撰写本文时)没有“ =>”运算符。

As a supplement to Jerry's great answer, I want to point out that this was not an oversight by any stretch, but a very conscious design decision. 作为对杰里的好答案的补充,我想指出,这绝不是疏忽,而是一个非常有意识的设计决定。 Bjarne Stroustrup, the original creator of the C++ language, describes his thoughts on this in his fabulous book “ The Design and Evolution of C++ ”, as I quote here: C ++语言的原始创建者Bjarne Stroustrup在他的精彩著作“ C ++的设计和演进 ”中描述了他对此的看法,我在这里引用:

I [Stroustrup] considered it important to provide overloading as a mechanism for extending the language and not for mutating it; 我[Stroustrup]认为提供重载作为一种扩展语言而不是对其进行变异的机制很重要; that is, it is possible to define operators to work on user-defined types (classes), but not to change the meaning of operators on built-in types. 也就是说,可以定义运算符以使用用户定义的类型(类),但不能更改内置类型的运算符的含义。 In addition, I didn't want to allow programmers to introduce new operators . 另外,我不想让程序员引入新的运算符 I feared cryptic notation and having to adopt complicated parsing strategies like those needed for Algol68.” 我担心采用隐喻表示法,不得不采用复杂的解析策略,例如Algol68所需的解析策略。”

(bold emphasis mine, italics in the original) (加粗强调,斜体为原文)

Source: Stroustrup, Bjarne: “The Design and Evolution of C++”, Addison-Wesley, 1994. §3.6.5 资料来源:Stroustrup,Bjarne:“ C ++的设计和演进”,Addison-Wesley,1994年。§3.6.5

PS: Although a bit dated as a reference for modern C++ design, this is an excellent and fascinating source to explore the history and the reasoning that led to the design of the original C++ language. PS:尽管过时了作为现代C ++设计的参考,但这是探索导致原始C ++语言设计的历史和推理的极好且引人入胜的资料。 The language further design has long been under the purview of an ISO Standards committee but its continuous evolution has continued to be driven by many of the same principles described in the book and Dr. Stroustrup continues to be an important voice in that evolution process. 语言的进一步设计长期以来一直由ISO标准委员会负责,但其不断发展一直受到本书中所描述的许多相同原理的推动,而Stroustrup博士仍然是该发展过程中的重要声音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM