Money has a member operator+ and an implicit Money(long long) constructor; why does m + 250 compile in C++ but 250 + m not?
answer
- who is on the left
- no conversion for the object
- free functions convert both sides
- =, (), [] and -> stay members
basics
~20 sA member operator+ is only found when the left operand is already a Money, and the implicit object never takes a user-defined conversion. 250 + m has an int on the left, so only a non-member operator+ makes the two orders symmetric.
solid answer
~40 sFor `x + y`, the compiler collects **member candidates** only from the class of the *left* operand, plus **non-member candidates** found by ordinary lookup and argument-dependent lookup. `m + 250` finds `Money::operator+` and converts `250` through the implicit constructor. `250 + m` has an `int` on the left, which has no members, and the standard never applies a user-defined conversion to the implicit object argument, so nothing matches and clang reports `invalid operands to binary expression ('int' and 'Money')`. The fix is a **non-member** operator, usually a hidden friend, where both operands are ordinary parameters. Some operators must be members: `=`, `()`, `[]` and `->`; since C++23, `()` and `[]` may also be `static` and `[]` may take several arguments. C++20's reversed candidates restore symmetry for `==` and `<=>`, never for `+`.
code
plaintext · 4 linessym.cpp:11:19: error: invalid operands to binary expression ('int' and 'Money')
11 | Money b = 250 + m; // error: the left operand is an int
| ~~~ ^ ~
1 error generated.go deeper
Know that operators can be members or free functions, and that =, (), [] and -> must be members.
Explain how member and non-member candidates are collected, why the left operand of a member is never converted, and the hidden-friend form.
Show why implicit converting constructors on value types cause surprises, how to restore symmetry with explicit overloads, and what C++20 and C++23 changed.
Set the team convention for operator placement and implicit conversions on value types, weighing convenience against silent unit and type mistakes.
## How the compiler finds an operator For a binary expression `x @ y` where an operand has class type, the standard builds three candidate sets and runs ordinary overload resolution over them: 1. **Member candidates**: `operator@` looked up in the class of `x`, the left operand, if it is a class type. A member `operator@` is called as `x.operator@(y)`. 2. **Non-member candidates**: `operator@` found by unqualified lookup and argument-dependent lookup, ignoring members. A non-member is called as `operator@(x, y)`. 3. **Built-in candidates**, such as `int + int`. The member's left operand is the **implicit object argument**, and the standard says no user-defined conversion can be applied to it. A non-member's two parameters are ordinary parameters, and both can take conversions. ## Why m + 250 works and 250 + m does not With `Money operator+(const Money&) const` as a member and a non-`explicit` `Money(long long)` constructor: - `m + 250`: the left operand is a `Money`, so the member is a candidate; `250` converts to `Money` through the constructor for the parameter. It compiles. - `250 + m`: the left operand is an `int`, which has no members, so the member set is empty. There is no non-member `operator+(Money, Money)`, and `250` will not be converted to become an object for a member call. The code example shows clang's diagnostic: `invalid operands to binary expression ('int' and 'Money')`. Making `operator+` a **non-member** fixes it, because then `250` converts as an ordinary argument. A common form is the **hidden friend**, defined inside the class with `friend`, which keeps the operator out of ordinary lookup so it is only found when a `Money` takes part. ## Symmetry without implicit conversions An implicit constructor from a raw number is a questionable design for money: it lets `m + 250` mean "add 250 cents" silently. With an `explicit` constructor, the symmetric case that remains is scaling, and it needs **two** non-member overloads, `operator*(Money, long long)` and `operator*(long long, Money)`, as in the code example. A member can only ever provide the first. ## Operators that must be members | Operator | Rule | |---|---| | `=` | must be a non-static member; clang: `overloaded 'operator=' must be a non-static member function` | | `()` | must be a member; since C++23 it may be `static` | | `[]` | must be a member; since C++23 it may be `static` and take any number of arguments | | `->` | must be a non-static member taking no arguments | Conversion functions such as `explicit operator bool() const` are members by their very form. Compound assignments (`+=`) are members by convention rather than by rule. The C++23 change to `[]` means `grid[row, col]` calls `operator[](int, int)`; before C++23 the comma inside brackets was a comma expression, deprecated in C++20. ## The C++23 forms of () and [] ```cpp struct Grid { std::array<int, 6> cells{}; int& operator[](int row, int col) { return cells[row * 3 + col]; } // C++23 }; struct Doubler { static int operator()(int x) { return 2 * x; } // C++23: no object needed }; ``` A `static operator()` removes the implicit object parameter, which helps stateless function objects. Before C++23, clang rejects the two-parameter subscript with `overloaded 'operator[]' cannot have more than one parameter before C++23`. ## What C++20 changed, and what it did not C++20 added **rewritten and reversed candidates** for comparisons. For `250 == m`, the compiler also tries `m == 250`, so a member `operator==` plus the implicit constructor makes both orders compile; the same applies to `<`, `>`, `<=` and `>=` through `<=>`. Nothing similar exists for arithmetic: `250 + m` still needs a non-member `operator+`. ## The rule to give in an interview - Make an operator a **member** when it must be one (`=`, `()`, `[]`, `->`) or when it modifies its left operand (`+=`). - Make **binary operators that treat both operands alike** (`+`, `*`, `==`, `<=>`) non-members, usually hidden friends, so conversions apply to both sides. - Prefer `explicit` constructors for value types such as money, and add exactly the mixed-type overloads the domain needs.
- Why can operator= not be a free function?The standard requires a simple assignment operator function to be a non-static member. A class always has a copy assignment operator, implicitly declared if you write none, and assignment to a class object is interpreted as a member call; clang rejects a free one with `overloaded 'operator=' must be a non-static member function`.
- What does a hidden friend add over a plain free operator+?A friend defined inside the class can read private members without public accessors, and it is found only by argument-dependent lookup, when a `Money` is an operand. That keeps it out of unrelated overload sets and shortens error messages. The lookup rules are covered with namespaces and lookup.
- In C++23, what does grid[1, 2] call?If the class declares `operator[](int, int)`, it calls that with two arguments; C++23 allows a subscript operator with any number of parameters. Before C++23 the comma inside the brackets was a comma expression, deprecated since C++20, so the same code meant `grid[2]`.
- A member operator+ converts its left operand like any other argument
- Every operator can be written as either a member or a free function
- C++20 rewriting makes 250 + m work with a member operator+
- operator[] can take only one argument in current C++
- A friend operator is the same thing as a member operator
Assumes C++23: static operator() and static or multi-argument operator[] are C++23, and reversed comparison candidates are C++20; the member and non-member lookup rules are unchanged since C++98.