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

  • A+

=> isn't overloadable in 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.

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. 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++. I wouldn't expect to see it as part of C++ any time soon (or, probably, ever).


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: