Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 800
steveo225 Avatar asked Nov 24 '25 09:11

steveo225


1 Answers

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.

like image 198
hmakholm left over Monica Avatar answered Nov 26 '25 22:11

hmakholm left over Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!