Skip to content
Surf Wiki
Save to docs
general/articles-with-example-algol-68-code

From Surf Wiki (app.surf) — the open knowledge base

Operator overloading

Feature of some programming languages


Feature of some programming languages

In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by a programming language, a programmer, or both.

Rationale {{Anchor|Motive}}

Operator overloading is syntactic sugar, and is used because it allows programming using notation nearer to the target domain and allows user-defined types a similar level of syntactic support as types built into a language. It is common, for example, in scientific computing, where it allows computing representations of mathematical objects to be manipulated with the same syntax as on paper.

Operator overloading does not change the expressive power of a language (with functions), as it can be emulated using function calls. For example, consider variables , and of some user-defined type, such as matrices:

In a language that supports operator overloading, and with the usual assumption that the operator has higher precedence than the operator, this is a concise way of writing:

However, the former syntax reflects common mathematical usage.

Examples

In this case, the addition operator is overloaded to allow addition on a user-defined type in C++:

Time operator+(const Time& lhs, const Time& rhs) {
    Time temp = lhs;
    temp.seconds += rhs.seconds;
    temp.minutes += temp.seconds / 60;
    temp.seconds %= 60;
    temp.minutes += rhs.minutes;
    temp.hours += temp.minutes / 60;
    temp.minutes %= 60;
    temp.hours += rhs.hours;
    return temp;
}

Addition is a binary operation, which means it has two operands. In C++, the arguments being passed are the operands, and the object is the returned value.

The operation could also be defined as a class method, replacing by the hidden argument; However, this forces the left operand to be of type :

// The "const" right before the opening curly brace means that `this` is not modified.
Time Time::operator+(const Time& rhs) const {
    Time temp = *this;  // `this` should not be modified, so make a copy.
    temp.seconds += rhs.seconds;
    temp.minutes += temp.seconds / 60;
    temp.seconds %= 60;
    temp.minutes += rhs.minutes;
    temp.hours += temp.minutes / 60;
    temp.minutes %= 60;
    temp.hours += rhs.hours;
    return temp;
}

Note that a unary operator defined as a class method would receive no apparent argument (it only works from ):

bool Time::operator!() const {
    return hours == 0 && minutes == 0 && seconds == 0;
}

The less-than (

class IntegerPair {
private:
    int x;
    int y;
public:
    explicit IntegerPair(int x = 0, int y = 0):
        x{x}, y{y} {}

    bool operator<(const IntegerPair& p) const {
        if (x == p.x) {
            return y < p.y;
        }
        return x < p.x;
    }
};

Like with the previous examples, in the last example operator overloading is done within the class. In C++, after overloading the less-than operator (`operator

Starting in C++20 with the introduction of the three-way comparison operator (operator), all the order operators can be defined by just defining that operator. The three-way comparison operator exists in many languages, including C++, Python, Rust, Swift, and PHP. Other languages, such as Java and C# instead use a method Comparable.compareTo().

import std;

using std::strong_ordering;

class IntegerPair {
private:
    int x;
    int y;
public:
    explicit IntegerPair(int x = 0, int y = 0):
        x{x}, y{y} {}

    // can be auto-generated with = default;
    strong_ordering operator<(const IntegerPair& p) const {
        if (strong_ordering cmp = x <=> p.x; cmp != strong_ordering::equal) {
            return cmp;
        }
        return y <=> p.y;
    }
};

Criticisms

Operator overloading has often been criticized because it allows programmers to reassign the semantics of operators depending on the types of their operands. For example, the use of the {{code|a shifts the bits in the variable left by bits if and are of an integer type, but if is an output stream then the above code will attempt to write a to the stream. Because operator overloading allows the original programmer to change the usual semantics of an operator and to catch any subsequent programmers by surprise, it is considered good practice to use operator overloading with care (the creators of Java decided not to use this feature, although not necessarily for this reason).

Another, more subtle, issue with operators is that certain rules from mathematics can be wrongly expected or unintentionally assumed. For example, the commutativity of + (i.e. that ) does not always apply; an example of this occurs when the operands are strings, since + is commonly overloaded to perform a concatenation of strings (i.e. yields , while yields ). A typical counter to this argument comes directly from mathematics: While + is commutative on integers (and more generally any complex number), it is not commutative for other "types" of variables. In practice, + is not even always associative, for example with floating-point values due to rounding errors. Another example: In mathematics, multiplication is commutative for real and complex numbers but not commutative in matrix multiplication.

Catalog

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

OperatorsNot overloadableOverloadableNew definableLimited set
website=geeksforgeeks.org}}

Timeline of operator overloading

1960s

The ALGOL 68 specification allowed operator overloading.

Extract from the ALGOL 68 language specification (page 177) where the overloaded operators ¬, =, ≠, and abs are defined:

10.2.2. Operations on Boolean Operands a) op ∨ = (bool a, b) bool:( a | true | b ); b) op ∧ = (bool a, b) bool: ( a | b | false ); c) op ¬ = (bool a) bool: ( a | false | true ); d) op = = (bool a, b) bool:( a∧b ) ∨ ( ¬b∧¬a ); e) op ≠ = (bool a, b) bool: ¬(a=b); f) op abs = (bool a)int: ( a | 1 | 0 );

Note that no special declaration is needed to overload an operator, and the programmer is free to create new operators. For dyadic operators their priority compared to other operators can be set:

prio max = 9;

op max = (int a, b) int: ( ab | a | b ); op ++ = ( ref int a ) int: ( a +:= 1 );

1980s

Ada supports overloading of operators from its inception, with the publication of the Ada 83 language standard. However, the language designers chose to preclude the definition of new operators. Only extant operators in the language may be overloaded, by defining new functions with identifiers such as "+", "*", "&" etc. Subsequent revisions of the language (in 1995 and 2005) maintain the restriction to overloading of extant operators.

In C++, operator overloading is more refined than in ALGOL 68.

1990s

Java language designers at Sun Microsystems chose to omit overloading. When asked about operator overloading, Brian Goetz of Oracle responded "Value types first, then we can talk about it.", suggesting that it could potentially be added after Project Valhalla.

Python allows operator overloading through the implementation of methods with special names. For example, the addition (+) operator can be overloaded by implementing the method .

Ruby allows operator overloading as syntactic sugar for simple method calls.

Lua allows operator overloading as syntactic sugar for method calls with the added feature that if the first operand doesn't define that operator, the method for the second operand will be used.

2000s

Microsoft added operator overloading to C# in 2001 and to Visual Basic .NET in 2003. C# operator overloading is very similar in syntax to C++ operator overloading:

public class Fraction
{
    private int numerator;
    private int denominator;

    // ...

    public static Fraction operator +(Fraction lhs, Fraction rhs)
        => new Fraction(lhs.numerator * rhs.denominator + rhs.numerator * lhs.denominator, lhs.denominator * rhs.denominator);
}

Scala treats all operators as methods and thus allows operator overloading by proxy.

2010s

In Raku, the definition of all operators is delegated to lexical functions, and so, using function definitions, operators can be overloaded or new operators added. For example, the function defined in the Rakudo source for incrementing a Date object with "+" is:

multi infix:<+>(Date:D $d, Int:D $x) {
    Date.new-from-daycount($d.daycount + $x)
}

Since "multi" was used, the function gets added to the list of multidispatch candidates, and "+" is only overloaded for the case where the type constraints in the function signature are met. While the capacity for overloading includes +, *, =, the postfix and term i, and so on, it also allows for overloading various brace operators: [x, y], x[y], x{y}, and x(y).

Kotlin has supported operator overloading since its creation by overwriting specially named functions (like plus(), inc(), rangeTo(), etc.)

data class Point(val x: Int, val y: Int) {
    operator fun plus(other: Point): Point {
        return Point(this.x + other.x, this.y + other.y)
    }
}

Because both Kotlin and Java compile to , when converted back to Java this will just be represented as:

public class Point {
    // fields and constructor...

    public Point plus(Point other) {
        return new Point(this.x + other.x, this.y + other.y);
    }
}

Operator overloading in Rust is accomplished by implementing the traits in std::ops.

use std::ops::Add;

#[derive(Debug)]
struct Point {
    x: i32,
    y: i32
}

impl Point {
    pub fn new(x: i32, y: i32) -> Self {
        Point {x, y}
    }
}

impl Add for Point {
    type Output = Point;

    fn add(self, other: Point) -> Point {
        Point {
            x: self.x + other.y,
            y: self.y + other.y
        }
    }
}

fn main() {
    let p1: Point = Point::new(1, 2);
    let p2: Point = Point::new(3, 4);
    let sum: Point = p1 + p2;
    println!("Sum of p1 and p2: {:?}", sum);
}

References

References

  1. Stroustrup, Bjarne. "Operator Overloading".
  2. Fisher, Charles N.. (2008). "Issues in Overloading". [[University of Wisconsin–Madison]].
  3. "No more operator overloading". [[Oracle Corporation]].
  4. Completely new operators can be added.
  5. "Predicate op/3".
  6. "Bertrand Meyer: Basic Eiffel language mechanisms".
  7. "Operator functions in F90".
  8. Introduced in Fortran 90.
  9. Smith, Chris. (9 October 2012). "Programming F# 3.0: A Comprehensive Guide for Writing Simple Code to Solve Complex Problems". O'Reilly Media, Inc..
  10. [[Type class]]es instead of overloading.
  11. "Operators - R in a Nutshell, 2nd Edition [Book]".
  12. "Operators".
  13. Hunt, John. (6 December 2012). "Smalltalk and Object Orientation: An Introduction". Springer Science & Business Media.
  14. "Swift: Advanced Operators".
  15. "Why does Go not support overloading of methods and operators?".
  16. "Introduction".
  17. "Operator Overloads".
  18. "6.6 Overloading of Operators".
  19. (2003). "C# in a Nutshell". O'Reilly Media, Inc..
  20. "C++ Operator Overloading".
  21. "Operator Overloading - D Programming Language".
  22. "A tour of the Dart language".
  23. "The Apache Groovy programming language - Operators".
  24. "Operator overloading".
  25. "Metamethods Tutorial".
  26. "Implementing Operators for Your Class".
  27. "Operator Overloading".
  28. "Operator Overloading".
  29. "PHP magic methods overriding class properties".
  30. Orwant, Jon. (4 November 2002). "Computer Science & Perl Programming: Best of The Perl Journal". O'Reilly Media, Inc..
  31. "3. Data Model".
  32. "Methods".
  33. "Operator Overloading".
  34. (15 September 2021). "How to: Define an Operator (Visual Basic)".
  35. (August 1968). "Report on the Algorithmic Language ALGOL 68, Section 10.2.2.".
  36. Stroustrup, Bjarne. "A History of C++: 1979−1991".
  37. "FAQ Question 6.9: Why isn't there operator overloading?".
  38. "java.sun.com".
  39. Holzner, Steven. (2001). "C++: Black Book". Coriolis Group.
  40. "Value types first, then we can talk about it.".
  41. "3. Data Model, Special method names".
  42. (13 June 2025). "Operator overloading - predefined unary, arithmetic, equality, and comparison operators". Microsoft Learn.
  43. "Operator overloading Kotlin". JetBrains s.r.o..
  44. "Operator Overloading - Rust By Example". Rust Programming Language.
Info: Wikipedia Source

This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.

Want to explore this topic further?

Ask Mako anything about Operator overloading — get instant answers, deeper analysis, and related topics.

Research with Mako

Free with your Surf account

Content sourced from Wikipedia, available under CC BY-SA 4.0.

This content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.

Report