Talk:Operator overloading

From Wikipedia, the free encyclopedia
Jump to: navigation, search
edit·history·watch·refresh Stock post message.svg To-do list for Operator overloading:

Here are some tasks awaiting attention:
WikiProject Computer science  
WikiProject icon This article is within the scope of WikiProject Computer science, a collaborative effort to improve the coverage of Computer science related articles on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
 ???  This article has not yet received a rating on the project's quality scale.
 ???  This article has not yet received a rating on the project's importance scale.

clarification: static vs dynamic[edit]

Doesn't overloading usually refer to the static resolution of a call? For instance, + on int vs String in Java is resolved statically. A dynamic analog of overloading is something like a type-case or virtual methods. So I think this article should focus on the static case, which means that some of the examples, like LISP, do not feature overloading, because they are statically untyped. —Preceding unsigned comment added by (talk) 01:57, 6 February 2008 (UTC)

I removed this text:

Similarly, and with operators in PostgreSQL, where mathematical transformations can be defined on operators and PostgreSQL may use many optimalizations to expressions that use them.

Because I couldn't decipher what the original author was trying to express. If someone can clarify the intent of this passage and wants to put the content back into the article, please do so. Neilc 02:26, 11 Aug 2004 (UTC)

more than implicit aesthetics[edit]

Operator overloading provides more than an aesthetic benefit when the language allows operators to be invoked implicitly in some circumstances. For example, this is the case with to_s operator in Ruby, which returns a string representation of an object.

This doesn't make sense to me. How is it more than aesthetic? Is there other difference if you just write the invocation? I think the point is there might be a difference between function call semantics and operator semantics (such as a + b might choose between a.add and b.add)

it depends upon if you assume that there exist other overloadable methods/functions/whatever or not. If operator overloading is the only overloading, it does add more than aesthetic.

By the way, you could add Lisp to the language classification :-)

Ok, I removed this until someone can explain how it makes sense. --TuukkaH 15:40:05, 2005-09-07 (UTC)

Declaring new operators[edit]

What is meant by "declaring new operators"? 10:20, 28 Nov 2004 (UTC)

Using operator symbols in the language that have no default meanings, like an operator *** or ## for instance. Pico allows this. Wouter Lievens 10:48, 6 Mar 2005 (UTC)


The criticism counts for all language features, so it is a red herring, imho.


a.addToList(b); // this function call sends an e-mail to Bill Gates

This is also an example of giving something a bad name (or bad semantics). The criticism is thus irrelevant. Wouter Lievens 10:48, 6 Mar 2005 (UTC)

It may be a red herring, but it is also the commonest criticism of overloading. The purpose of Wikipedia is to describe things as they are, not to evaluate them: our job is to report what critics actually say, relevant or not, not to try to evaluate whether things are good or bad ourselves. Haeleth 14:49, September 6, 2005 (UTC)

First language with Operator Overloading[edit]

It would be interesting to identify the first language to introduce operator overloading. Anyone know if any language before ALGOL 68 (1968) implemented it?

Rwendland 02:05, 6 Apr 2005 (UTC)

Gutter talk?[edit]

I've removed the "gutter talk" section, which (if you can't be bothered to check History) was about how what absolutely everyone calls "operator overloading" should really be called "operator overriding" unless a class exports more than one operator. Given that this is not a common criticism, nor is it supported by actual usage, I don't really think it's encyclopaedic content. (It was also of disputable accuracy.) Haeleth 14:49, September 6, 2005 (UTC)


In lisp, (+ 3 5) adds 3 & 5. Lisp uses the same syntax for functions: (funcname arg1 arg2) etc. Because of this, defining new operators in lisp is as easy as defining a new function. Overriding an operator is the same as defining a new function. Because of this, I changed lisp's position in the chart to indicate that operators are overloadable and new operators are definable.--Dasunt 23:27, 3 March 2006 (UTC)

This is true only if you can have the function + converted into a generic, so you are later able to overload it for different classes. If you only override the previous function definition of the symbol + you're not overloading it. I think it can be done, but am not sure. --euyyn 22:25, 2 July 2006 (UTC)
As Lisp has dynamic typing and first class functions this is trivially true. You could assign the old implementation to some value, and then define a new function that checks the type of the argument and applies eighter the old implementation or a new one depending on the result. (CLOS also has inbuilt generic functions, but you dont realy need them to do overloading in lisp).
Correct me if I'm wrong but aren't these all just functions in Lisp? Just because a function uses a symbol like "+" does not really make it an operator. I mean if I have a function in Lisp that does the same thing as "+" but is called "a", I don't see how you can say "+" is an operator but "a" is not. I think we have to draw a distinction that operators are something different to functions, and have things like (say) associativity, precedence, etc. There has to be more to it than just whether it uses a letter or a symbol as its glyph. Again, I'm no Lisp guru, but it seems to me that Lisp really has no operators, so talking about it in an article about operator overloading doesn't really make sense. A scientist (talk) 06:27, 20 January 2009 (UTC)
Lisp definitively only has functions and not operators, much less operators with precedence and associativity. The justification that "operators use the same prefix syntax as functions" is a very feeble argument. With that argumentation every language supporting overloading of functions would also count as having operator overloading. Therefore I remove the entry of Lisp from the catalog. Zron (talk) 16:00, 12 August 2010 (UTC)


How is it that Ruby permits new operators to be defined? It seems to me that only existing operators that are part of the syntax can be defined as methods in Ruby. If that's not what that axis of the chart means, perhaps it should be clarified.

You're right. We can't define new operators in Ruby. There's a limited set (language's operators, except the following: =, .., ..., !, not, ||, &&, and, or, ::). I'm going to remove it from the list. 01:02, 18 February 2007 (UTC)

Dubious statement regarding symbols and citations needed[edit]

The ISO/IEC 2382 Standard Vocabularies for IT define an operator as a symbol that identifies or represents an operation. The definition does not limit symbols to single characters. Thus '+', 'add', 'first', 'next' could all be operators.

It is in fact the symbol that is overloaded through reuse. While, hopefully, the operations have similar semantics, the operations are in fact different.

A more accurate statement would be: "Operators are not limited to single characters." As it is, this article appears to contradict a fundamental computing definition and to confuse a symbol with what it represents.

The article lacks citations for verifiability and runs the risk of violating No Original Research or No Point of View.

Bob Badour 15:22, 29 June 2006 (UTC)

Come on, in the sentence, the word symbol refers to what everybody understand as a symbol, i.e., a graphical/printed sign (in opposition to letters and numbers), not to the special meaning we in the field of programming also have for it. --euyyn 22:47, 2 July 2006 (UTC)
Clearly not everyone understands the word symbol the same. It should be clarified to show the intended meaning, perhaps with something like "punctuation characters," "graphical symbols," or "non-alphabetic characters."-- 16:06, 9 July 2006 (UTC)
Isn't the statement referring to something like C++'s conversion operators? 12:33, 7 July 2006 (UTC)

samples for expression reduction?[edit]

are there any known languages that support expression reduction (outside of the compiler)? —The preceding unsigned comment was added by (talk) 23:45, 17 January 2007 (UTC).

Correction with Java[edit]

Java allows method overloading, hence it should be moved to another part of the graph. --indil 08:48, 11 April 2007 (UTC)

Java allows only method overloading, that is different to *operator* overloading. (talk) 18:24, 13 August 2008 (UTC)

What is an operator?[edit]

Which properties decide about the classification as operator. IMHO there are some points:

  • An operator can consist of letters or special characters.
  • There should be no extra characters required around the operator.
  • An operator can be classified as prefix, infix or postfix.
  • An operator has a priority (or precedence) and an associativity.

There are several notations which do not count as operators:

  • A C function which is called with "cos(x)" requires parentheses and has no priority or associativity.
  • A Lisp function which is called with "(cos x)" requires parentheses and has no priority or associativity.
  • A Smalltalk method which is called with "b at: 3" requires a colon and has no priority or associativity.
  • A Java method which is called with "collection.remove(obj)" requires a dot, parentheses and has no priority or associativity.
  • It is also not an operator when the operator symbol is required to be surrounded by dots (e.g.: FORTRAN .AND. ).

In the article several me too languages claim to have operators (IMHO they don't). Such languages should be mentioned in other articles but not here.

When there is no clear distinction about what counts as operator some information like the table about user definable / overloadable operators make not really sense. Only a few languages use real operators and allow user definable new operator symbols with priority and associativity. Because of the many me too languages this information is not clearly visible. Georg Peter 07:17, 30 October 2007 (UTC)

I think the key point is that operators have implicit associativity and priority rules. The notatonal convention is irrelevant. As such, Lisp doesn't have operators at all, it has only functions. I think some distinction has to be made between function/method overloading and operator overloading. --Tweenk (talk) 12:53, 17 November 2007 (UTC)
I'm inclined to agree with Georg Peter on the issue of "priority and associativity" only, however, I can't find any good reliable supporting documentation. In fact, even C++, which should be the poster child for "what defines an operator" says that "sizeof" (usage: sizeof(var);) is an operator. Conversely, however, C++ does not allow you to define new operators (not to be confused with overloading operator new) implying that my own functions are not considered operators (making sizeof an oddball, since it looks like a function, but is somehow an operator?) There appears to be no standard on this other than the arbitrary whims of the folks that designed any particular language.
I think the real question here is "What does the programmer mean by Operator Overloading?" This isn't terribly hard to decide on. If my language of choice calls something an operator, but allows me to redefine how it works, I am overloading it. Creating a custom operator is totally different from overloading an existing operator (the topic of this article.) If we stick to overloading only, we can focus only on those symbols that a language already calls "operators" and avoid the headache of having to define (and fight over the definition of) what actually constitutes an "operator." This won't resolve the question, but it would relocate it to an article more suited to hashing it out (like operator.) --JCrenshaw (talk) 06:23, 15 December 2008 (UTC)
So when a language uses the name operator instead of variable you would still aggree? What about a language which speaks of overloading instead of local variables vs. global variables? I dont't think that this approach (we just take what a language calls operator) is a good idea. There is a definition of what can be called operator and this definition should really be outside of a particular language. Zron (talk) 12:14, 27 April 2011 (UTC)


We have a bad example; adding two dates doesn't make sense, just as adding two pointers doesn't. We should change it into adding two durations of time, or two lengths, etc. — Musiphil (talk) 19:07, 18 March 2009 (UTC)

Done. — Musiphil (talk) 07:27, 23 March 2009 (UTC)
Please accept my comments in the spirit offered?

Adding dates or pointers makes perfect sense for some applications. My concern is more practical in nature.

I would make the changes myself if I understood operator overloading and classes, but I never needed to use them in my work. In some ways that makes me the perfect audience but I cannot figure out the examples because:

1 I never used operator overloading and the concept is alien to me, as is classes

2 the examples do not have a specification explaining in plain language exactly what they are supposed to be doing to which nor are the special data types defined

3 the examples lack line-by-line comments exactly what each line is accomplishing

4 the code is not 'self-documenting' directly from the syntax, especially in the third example which I cannot even make out what its intent is let alone what it is doing

I think we all get the idea of operator overloading being subtle and confusing due to syntax, but that is not what is making this code difficult to understand nor is it pointing out to me why I should care.

Could you please add some declaration of data type and plain language pecification/description of the intended input, operation, and output of each of the three examples? Lacking that, how about line-by-line comments and a general description of what the third cryptic example does?

Then, how about pointing out specifically what is important about the operator overloading, with respect to the second and third examples?

Specifically, I fail to see what the point is for using a 'hidden' variable in the second example. I consider it poor coding style to use global variables without a good reason and in this case it only seems to be justifying the need for a class instead of a simple overload.

What about the global variable justifies the need for a class? Is it the lack of a lhs operand for a typically binary operator? If so, can we have sample instantiation because otherwise the implication is that it uses the same instantiation as the first example? Please state it clearly because it still has me baffled, like I said I never used classes, I did not need object oriented code. I would need to understand everything about classes and operator overloading already to understand the example and in that case what is the point of the encyclopedia?

In the third example I do not even know what it is for. I see no connection between it and the prior two examples, nor is there an instantiation or declaration of data type or anything, nor do I see that it even accomplishes anything since it seems to set everything to zeros. What are its operands? It seems like a ghost that returns no output at all.

Maybe too you could mention some practical implications of the chosen syntax for the overload and alternative coding to mitigate, like what I had to be concerned with such as for example (maybe not my specific concerns for these operator overload samples but similar in character to):

1 potential surprise pointer overflows from improper boundary conditions, and how to prevent this without relying on the compiler to catch it for you, letting J-bird Hacker Jones hijack your browser with data execution and clean out your bank account

2 unexpected processor or memory or stream overhead while executing the code due to practical hardware limitations on bandwidth, flushing the cache for unnecessary jump instruction, multiplying by 2 instead of left shifting, etc.

3 automatic compiler error-checking overhead that might get wrapped around the assembly language, that either mitigates or causes such non-transparent issues, and how to avoid the need for relying on the compiler to clean up sloppy code for you

4 'if' statements instead of modulo operators makes the code longer and more confusing since no one would ever code this way unless processor bandwidth or some other practical hardware constraint demanded it. Can we have examples that make sense mathematically and functionally, so that we can concentrate on the issue of operator overloading?

C syntax is confusing enough without leaving people that should be able to read this encyclopedia entry with ease scratching our heads in puzzlement. Please? thx —Preceding unsigned comment added by (talk) 13:51, 21 January 2010 (UTC)

Catalog: Classification as either "new definable" or "limited set"?[edit]

"A classification of some common programming languages by whether their operators are overloadable by the programmer and whether the operators are limited to a predefined set."

Is this really an either/or situation? Are there no programming languages which allow both overloading existing operators, and defining new ones? After all these years I finally ran into a situation where having both features might be desirable in limited cases. Since my available tools are already pretty much set in stone, this is pretty much just an academic question, but I'm still interested. — Preceding unsigned comment added by (talk) 21:37, 31 October 2011 (UTC)

Look at the chart again - it has 4 sections, not 2. For example, Haskell lets you overload operators via type classes and you can use functions as infix operators, or even make them infix by default if you usually want to use them as operators (mod 5 4 is the same as 5 `mod` 4 and 1 + 2 is the same as (+) 1 2). strcat (talk) 12:05, 2 November 2011 (UTC)
Thanks, I obviously glossed over the chart and misunderstood it. Turns out, since the label "Limited set" isn't perfectly identical to the explanation "limited to a predefined set", and the most "flexible" languages aren't at the bottom right (or top left?) of the chart, my internal pattern recognition went haywire. Maybe the chart layout is just a tad counter-intuitive, even though it is clearly correct. — Preceding unsigned comment added by (talk) 17:09, 9 November 2011 (UTC)


The phrase "claimed to be useful" is not NPOV and uses weasel words. I will remove it. Dpsanders (talk) 05:06, 19 August 2013 (UTC)

Remedial description[edit]

Is there a chance that someone can take a step further back in the discussion and try to explain this to a more novice programmer? I am one such programmer (FORTRAN off & on for many years, Pascal, BASIC) but I have no idea what the author is saying. Operator Overloading remains a mystery to me after reading the article.

Thanks for considering — Preceding unsigned comment added by Mark B 64151 (talkcontribs) 00:17, 4 December 2014 (UTC)