Search code examples
c++operator-overloading

Create my own operator in c++


I know it is possible to overload operators that already exist in c++ to define desired behavior, but is it possible to create your own operator?

Just for example, making an operator # that returns the size of containers:

template<typename T>
size_t operator#(const T& obj) { return obj.size(); }

vector<int> v(1024);
cout << #v; // prints 1024

Solution

  • No. You need to stick with the operators the parser already knows how to handle. Overloading can extend the meaning of expressions, but the syntax is fixed.