On Friday, November 18, 2022 at 11:40:43 AM UTC+2, Juha Nieminen wrote:
> Michael S <
already...@yahoo.com> wrote:
> > For language that is so similar (nearly identical) to C at the level of
> > "virtual machine", as is C++, the concept of reference is just unnecessary.
> If we start dismissing everything that one could deem "unnecessary" then we
> could start dropping off quite many features even from C itself.
>
> After all, for example the syntax 'a[b]' is just syntactic sugar for
> '*(a+b)', and thus the former is "unnecessary" and could just be dropped
> off.
The [] variant is shorter by 2 characters. In DRM view, shortness of most
frequently used language features was of high priority.
Also, in slightly more complex expressions, in case of *(a+b) variant reader
has to look for more information in order to figure out which of two homonyms
of * is actually used. I would speculate that DRM was not really pleased with
using asterisk for dereference, but had no better choice in available
character set.
A 'for()' statement is unnecessary because you can write the same
> thing using a 'while()' statement.
Except that 'continue' behave differently.
The better argument is that 'while' loops are unnecessary and that is
absolutely correct. A mistake on part of DRM.
> And obviously the 'do' keyword is completely unnecessary.
I don't see it. Please elaborate.
> As well as the 'switch' keyword.
What alternative do have in mind? if-else-if chains?
Both more typing and intentions of using the same selection variable
for all choices is not pronounced clearly.
> And why do we
> even need a 'const' keyword at all? That could be easily dropped off.
People argue for that. IIRC, 'const' is relatively late addition to C, so
likely DRM was not very sure about it.
Personally, I find it useful, primarily for documenting of intent.
The claim that it often helps to catch bugs is hard to prove.
As to C++ [mis]use for peeking one of polymorphic methods... well,
it's pretty bad, but not the worst thing caused by polymorphic methods.
>
> We could easily drop half of C syntax as "unnecessary" and still be able
> to write the same programs as before. Not to talk about C++!
So, you don't think that C++ will become a better language if 2/3rd of it
is dropped? I was under impression that just about everybody agree about
that. Of course, they rarely agree about what third to retain.
> > Don't use references in your code except when forced by interfaces that
> > are out of your control! Then people that are going to read and comprehend
> > your code will be thankful.
> Actually references add useful abstraction to interfaces. For example,
> when you write 'foobar(abc)', is that 'foobar' function taking the
> parameter by value or by reference? It can decide! Whatever is best in
> that particular function, it can choose. The calling code doesn't need to
> tie its hands and force one or the other.
>
> Moreover, if later it turns out that the function would need to change
> how it takes the parameter (for example it originally took it by value,
> but the type got a lot larger after a refactor, so now it would be more
> efficient to take it by reference), it can do so without breaking any
> code that's calling the function. (And no, "just fix all the places
> where the function is called, by following the compiler errors" is not
> a good solution because this may be a library used somewhere, and you
> will be breaking the API, causing it to become incompatible with the
> previous version, forcing every project that uses the library to fix
> all the calls.)
>
If only const references were allowed as function arguments I'd
wholeheartedly agree with majority of above paragraph.
Unfortunately, mutable references are as legal as const ones
and the reader of the code has no hint about which variant was
used by looking at function call.
> Also, how do you suggest implementing copy constructors and operator
> overloading without references?
How said that I suggest implementing copy constructors and operator
overloading?
In my view, non-trivial constructor is original sin of C++ language.
It begot exceptions and the rest of the mess followed.