CKTIS / C++
operator — operator overloading and conversion functions
The operator keyword forms names for overloaded operator functions, conversion functions, and literal operators. It lets you define how operations work on your own types, without changing operator precedence or grouping.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
ReturnType operator+(const Type& rhs) const;
explicit operator bool() const;
ReturnType operator""_suffix(unsigned long long value);operator is part of a special function name, not a standalone operation. For example, operator+ defines addition for a user-defined type. In the example, Score{2} + Score{3} calls a member function on the left operand; its rhs parameter represents the right operand. You can also call that function explicitly, for example a.operator+(b). Depending on the operator, an overload can be a member or a non-member function.
Overloading an ordinary operator requires a class or enumeration type to participate; you cannot change addition of two int values. You cannot invent new operator symbols or change their precedence, grouping, or operand count. The operators ., .*, ::, and ?: cannot be overloaded. The functions operator new and operator delete handle memory allocation and deallocation under separate rules.
operator bool() is a conversion function: it has no separately written return type and is a non-static member function. explicit prevents ordinary implicit conversions while still allowing the object as an if condition. Another form, operator""_suffix, declares a namespace-scope literal operator; it is not an arithmetic operator overload.
The example uses C++23-compatible syntax. The source is an evolving working draft, not a frozen C++23 publication. The compiler flag selects the language mode; it does not attest production runtime compatibility.
Examples
#include <iostream>
struct Score {
int value;
Score operator+(const Score& rhs) const {
return Score{value + rhs.value};
}
explicit operator bool() const {
return value != 0;
}
};
int main() {
const Score total = Score{2} + Score{3};
std::cout << total.value << '\n';
if (total) {
std::cout << "nonzero\n";
}
}
Result
5 nonzero
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.