开发者

user-defined operators in C++

开发者 https://www.devze.com 2023-02-13 12:00 出处:网络
What do you think about an imaginary possibility to specify user-defined operators in C++. Such user-defined operator could be defined by operator name (arbitrary sequence o开发者_如何转开发f allowed

What do you think about an imaginary possibility to specify user-defined operators in C++.

Such user-defined operator could be defined by operator name (arbitrary sequence o开发者_如何转开发f allowed chars?), its precedence, associativity and arity (something else?).

They could be used for many purposes: to help to build "tiny" DSLs over C++, for list comprehension etc.

Wouldn't this feature extend the language possible usage? What are other languages that allow user-defined operators? Lisp comes to mind, anything else? Any links about the topic?


Well, you know, Bjarne Stroustrup did propose such a thing...Generalizing Overloading for C++2000. :-P :-P :-P


Well, Haskell has custom operators with settable precedence and left-right binding. So, it can work. But then, Haskell is cutting edge and barely readable as is, even though it's mostly used by some rather clever people. (Haskell scares off all newbies, I think..)

For C++, I think there are:

  • parsing issues (consider the std::vector<std::list<int>> bug, where >> was parsed as the right-shift operator) .. C++'s syntax is hard enough as is.
  • backwards-compability issues (introducing new operators that are combinations of old, like !-- could cause problems)
  • clarity issues (people are doing enough wierd thing with the regular operators, making the behaviour of a program difficult enough to divine as is.)

The latter one is the dealbreaker, IMO.

Nevertheless, nothing is stopping you from writing a c++-preprocessor/parser that replaces your own-defined operators with real function calls and then uses the normal c++ compiler (like how c++ was built on C previously). Would be a neat experiment, if you'd keep your sanity long enough to ship. ;-)


I fail to understand the advantage of such operators. Functions and methods are sufficient for every kind of use I can think of.

I think such a possibility would only make C++ a lot more complex and reduce readability of sources. Operators overriding is already such a mess in some sources that I can't imagine what some people would do with operators definition...

BTW, I really don't understand what you mean by "tiny DNSs over C++"


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

Now you have some code like this:

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

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

Is the compiler going to know whether the ** is as above or trying to do a "power" (and complain that the right hand side is a pointer).

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


You may be mixing concepts - user defined operator overloads are ok. This is because the compiler already knows the syntax rules for C++ defined operators.

User defined operators "per se" - no. Imagine being able to make the word "Klingon" an operator - how will it differentiate between operator and variable?

0

精彩评论

暂无评论...
验证码 换一张
取 消