Why does C ++ not allow user statements?

I’ve been interested in this for quite some time. They already have a whole bunch, and they can be overloaded, so why not do it to the end and allow user statements? I think this can be a great addition.

I was told that this would make the language too complicated to compile. It makes me wonder if C ++ cannot really be designed for easy compilation anyway, is it really impossible? Of course, if you use an LR parser with a static table and grammar like

E → T + E | T T → F * T | F F → id | '(' E ')' 

it will not work. In Prolog, which is usually analyzed using the AFAIK operator analyzer, you can easily define new operators, but the language is much simpler. Now the grammar can obviously be rewritten to accept identifiers in all places where the operator is hard-coded into the grammar.

What other solutions and parser schemes exist and what other things influenced this design decision?

+4
source share
7 answers

http://www2.research.att.com/~bs/bs_faq2.html#overload-operator

The opportunity has been considered several times, but each time we / we decided that probable problems outweigh the probable benefits.

This is not a language problem. Even when I first examined it in 1983, I knew how this could be implemented. However, my experience is that when we go beyond the most trivial examples, people seem to have subtly different opinions about the “obvious” meaning of using an operator. A classic example is a**b**c . Suppose ** was done to indicate exponentiation. Now a**b**c means (a**b)**c or a**(b**c) ? I thought the answer was obvious, and my friends agreed - and then we found that we did not agree on which resolution was obvious. My hypothesis is that such problems will lead to subtle errors.

+10
source

It would be harder to compile than what is already there. In addition, there would be problems with operator precedence: how do you define it? You need a way to tell the compiler that a user statement takes precedence over another statement.

It's almost certainly possible, but I think C ++ doesn't need other ways to shoot in the foot :-)

+2
source

This will make the language even more complex. And this is clearly not desirable.

However, check out Boost Spirit . This is a long way to do things like you mentioned using lots of template metaprogramming templates.

+1
source

It is actually designed for very simple analysis and compilation. C has 32 specific keywords, all other tokens are functions and variables.

There are several more in C ++. You can easily determine which token is for it, so know what to look for when you use a + token or something else.

0
source

The problem with providing custom statements is that you must also let the programmer specify the syntax of how the statements should be used. I believe a system like C ++ might help a little, but it will help solve problems like associativity, etc.

This will make an already complicated language, much more complicated ...

0
source

This is usually avoided because most of the code is written by more than one person, so the code should be “reviewed”, and this is hardly the “desired” feature of the language.

Joel Spolsky is a good article about this.

0
source

I just found out that you can actually achieve something very similar to overloaded operators. Consider

 Vector v, a, b; v = a /vectorProduct/ b; 

It turns out that you can achieve the behavior of a user-defined operator using dummy classes limited to existing operators. =)

0
source

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


All Articles