The mysterious –> operator in C++

The C++ –> is used for … well. I came across this post on Stackoverflow where someone asks what the –> operator in C++ is for, with the following code snippet:

I asked ChatGPT what the –> operator is, and it correctly stated that it “appears to use a non-standard and unconventional usage of the --> operator, which is not a standard operator in C++.” – which is quite good considering the hallucination it often shows. Furthermore, it says:

In C++, the --> operator doesn’t have any predefined meaning or behavior. Instead, it appears that the code you’ve shown is trying to use the -- (decrement) operator followed by the > (greater than) operator. However, this is not valid C++ syntax.

This is pretty much in line with the answer of the post on Stack Overflow.

Just for the fun, I asked ChatGPT how an implementation would look like if it was a valid operator, and this is what it came up with:

Now I asked it if it could imagine what the –> operator could be used for. Here is the answer:

Let’s imagine a use case for the --> operator in a hypothetical scenario:

Suppose you are working on a complex numerical simulation or mathematical modeling library in C++. In such a library, you might frequently work with mathematical expressions and matrices. You could use the --> operator as a custom operator for matrix operations, specifically for updating matrix elements in a concise and intuitive way.

It also mentions I should keep in mind that this is entirely speculative, as the --> operator doesn’t exist in the C++ language, and inventing custom operators should be done judiciously and with care for code readability and maintainability.

Then it produced some code:

  • The --> operator is overloaded for the Matrix class to update individual elements of the matrix.
  • The operator takes a pair of indices (row and column) and returns a reference to the corresponding matrix element.
  • It performs bounds checking to ensure that the indices are within the matrix dimensions.

Well, if C++ had a --> operator, we’d probably have debates about whether it should be called the “rocket ship operator” or the “arrow arrow operator.” And we’d have t-shirts with clever slogans like “I put the ‘fun’ in ‘function'” for C++ developers who love their custom operators. But for now, let’s stick with the good ol’ . and -> and leave the hypothetical operators to our wildest coding dreams! 😄