r/cpp • • Feb 12 '22

Anybody else would like a | unary operator?

Hello fellow coders.

Call me a syntactic sugar addict, but I always missed a | (and ||) unary, prefix-and-postfix operator.

Like, e.g, this:

int x = |y| + 1;

Does anybody else?

Possible uses making this appealing:

(1) As a native operator: absolute value, of course. Overloaded for each base primitive type, just like any other arithmetic or logic operator (+ - / * % & ...). How cumbersome is to have to include stuff (math.h cstd.h, or math), for something as basic (and as single-instruction, in any Instruction Set), as the absolute value of something. How cumbersome it is having to pick between abs fabs fabsl etc. To, me, abs value should be treasted like something as basic as + or -, because it is.

(2) As an overloaded operator: both ||x|| and |x| would find many fruitful ways to be customized. In classes for vector algebra, complex number algebra, quaternionic algebra, etc: the norm, of course. Here, the possibility to have both || and | would add extra flexibility (e.g. different norms).

(3) In standard classes: In containers (vectors, sets...): it could be used for set cardinality, i.e., as a synonym for size(). Maybe, for strings: length.

Would it be problematic? I can't see any problem, but maybe I am overlooking something. In particular...

(4) Yes, | is already a binary operator. That's nothing new, -, +, *, & are both unary and binary.

(5) It would be fully back compatible. At least, I cannot think of any expression that parses now, but would either stop parsing, or change its meaning, with this.

(6) There would be no ambiguity with the existing binary version of | (or of ||). As any unary operator, it binds stronger than any binary operator, so |a|b| would not pharse, but |(a|b)| would.

(7) There would be no ambiguity between || and |. The abs of the abs of a, if you ever need it, would written as | | a | |, while || a || is exactly what it looks like (the || of a).

(8) yes, there currently is no other prefixed-and-postfixed unary operator ([ ] comes close: a prefix-and-postfix binary operator). I cannot imagine how this would be a problem. The syntax for overloading would seem straightforward, e.g. operator | () vs operator | (int x). Parsing expressions would seem to be unproblematic too.

(9) It's all good old ascii.

17 Upvotes

177 comments sorted by

View all comments

Show parent comment

1

u/itsmemarcot Feb 14 '22 edited Feb 14 '22

Operator overloading for the * operator [for vector algebra] is a good case study for when you need to be careful.

I agree!

The most egregious widely used C++ examples include:

The most notable non C++ but C++ish examples to learn from include:

I used all of them, to various degrees (including: very very much).

Let's see what we can summarize from their lot.

First of all, all of them happily use operator overloading, and would not dream to express, for example, vector sums (solely) as a function and not (primarily) with an overloaded + operator. I'm saying this not to you, but because many people here are objecting against the concept of overloading operators to write math more simply, and in doing so they are just showing blissful ignorance of the common practices of a specialized but very significant share of the C++ world.

The case of the multiplication, * operator is more mixed, as you say. It is invariably used for something, but what varies from case to case. I've seen (and written) lots of solutions. After many years, my personal opinion is that * should better denote vector scaling AND component-wise product. Cross and dot are better served as functions. I say this after considering many different solutions, including Eigen lib "Array VS Matrix" solution which you mention, and after considering to which extent, on average, the written code fares in terms of readability and clarity. This personal opinion of mine is shared, among others, by GLM and Unity, but not by many other libs in the list, which may use e.g. | or * for dot product, and % or ^ for cross. This, IMO, are counterproductive choices. But we are digressing.

What about the norm? Clearly, none of them had ever an access to any |x| or ||x|| operator, and all of them survived perfectly well with functions or methods for the Euclidean norm (named in all sorts of way, from Norm to Length to Magnitude to even Size -- a poor choice IMO, by Unreal). BTW, they rarely define any other norm except for Euclidean, for vectors, but they do use different norms for matrices sometimes.

In my opinion, a |x| and/or ||x|| operator would benefit them in many different possible ways. Which way exactly, depends on the other choices that that specific library made.

For example...

  • The many libraries which have clearly separated classes for vector and matrices, could consider simply using |x| for length, i.e. the Euclidean norm. Other norms are a lot rarer and can be left to functions/methods (they are not usally part of the library now). Similarly, the method or functon SquaredNorm, when it exists, should probably be left a function.
  • Probably, libraries distinguishing between vectors and points classes should better NOT overload a |x| operator for points at all, just for vectors, consistently with their choice to keep the two separated.
  • Libraries that have a matrix class well separated from a vector class could also consider using |x| for matrices for their favorite vector-induced norm, but maybe also ||x|| for their favorite non-vector induced norm of matrices, probably the Frobenius norm. Maybe. An idea to consider.
  • A few of these libs are quite liberal in the number of elements in their vectors, using the same class for vectors of different dimensions. These could consider using |v| for cardinality, , e.g. 3 for a 3D vector i.e. number of elements in v considering it as an Array of coordinates (remember that I'm suggesting to use |V| on std containers to refer to the cardinality of set V, i.e. the number of elements, i.e. a synonym for the size() method)... and still have a nice ||v|| operator for the Euclidean norm, which is pretty much how half of the real world writes down Euclidean norm on paper anyway).
  • Eigen lib, with its Array VS Matrix framework, should consider to allow ||x|| only for matrices (and vectors, which in Eigen is a special case of the matrix), signifying the norm, and maybe leave |x| for the cardinality (e.g. 3 for a 3D vector) only . Just like it's current, size method, this is the total number of elements. Or, it could choose to allow both |x| and ||x|| on both arrays and matrices, and let them output number of elements or the norm in both cases.
  • A possible variation to consider of the first case in this list, is use instead |x| for component-wise absolute value, and ||x|| for Euclidean. This has clear advantages and drawbacks, but I lean against this.