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
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.
No, it is not possible.
Anyway hat would happen if you call your container "define" ?
You can create a new type (call it Foo
) and then create a Bar::operator Foo()
that converts a Bar
to a Foo
.
That probably is not what you were looking for.
You cannot make your own #
operator. Just as a starter, where would it fit in the (already terribly bloated) C++ precedence table? Does it have left to right or right to left associativity? How are you even going to specify these things, and more?
There are so very many problems associated with creating a brand spanking new operator that the language simply does not allow it.
As already answered, NO! You only can overload the all known operators and, by the way, you can't alter the behavior of the predetermined operators, then you can't change the behavior of the operator '+', by example, for the int data type. ;)