User Defined Operators in C ++

What do you think of the imaginary ability to specify custom operators in C ++.

Such a user-defined operator can be defined using the name of the operator (an arbitrary sequence of allowed characters?), Its priority, associativity, and arity (something else?).

They can be used for many purposes: to help create “tiny” DSL files on top of C ++, to understand lists, etc.

Wouldn't this feature expand the use of the language? What other languages ​​exist that allow user-defined operators to be defined? Lisp comes to mind, anything else? Any related links?

+4
source share
5 answers

Well, Haskell has custom operators with priority set and left-right binding. That way it can work. But then, Haskell has a cutting edge and is barely readable as it is, although it is mostly used by some pretty smart people. (Haskell scares away all newbies, I think ..)

For C ++, I think there are:

  • problems with analysis (consider the error std::vector<std::list<int>> , where >> was parsed as a right-shift operator). The C ++ syntax is pretty complicated as it is.
  • feedback problems (introducing new operators that are combinations of old ones, for example!) can cause problems)
  • problems with clarity (people do a rather strange thing with ordinary operators, making the program’s behavior difficult enough for the divine as it is.)

The latter is a robber, IMO.

However, nothing prevents you from writing C ++ - a preprocessor / parser that replaces your own operators with real function calls, and then uses the regular C ++ compiler (for example, like C ++ was built in C earlier). There would be a neat experiment if you kept your sanity long enough for delivery .; -)

+4
source

Well, you know, Bjarne Straustrup did offer such a thing ... Summarizing the overload for C ++ 2000. :-P :-P :-P

+6
source

I do not understand the benefits of such operators. Functions and methods are sufficient for any kind of use that I can think of.

I think that this feature will only simplify C ++ and reduce the readability of sources. Switching operators is already such a mess in some sources that I cannot imagine what some people will do with the definition of operators ...

By the way, I really don't understand what you mean by “tiny DNSs over C ++”

+1
source

So, let's say you define ** to mean "into power" as a user-operator.

Now you have this code:

 double d1 = 2.5; double *pd1 = &d1; double d2 = 3.0**pd1; 

Without the "operator **", the above code is really legal, it is parsed as 3.0 * (*pd1) (and the result is 7.5)

Will the compiler find out if this is the case as above, or is trying to make "power" (and complains that the right side of the pointer).

Having said that, I think that &&= and ||= and even ^^ and ^^= should be added as operators. (I don’t think ^^ is logical xor, giving true if exactly one of the expressions is non-zero).

+1
source

You can mix concepts - custom operator overloads are fine. This is because the compiler already knows the syntax rules for C ++ specific operators.

User statements "as such" are not. Imagine that you can make the word "Klingon" an operator - how will it distinguish between an operator and a variable?

0
source

Source: https://habr.com/ru/post/1341480/


All Articles