News for hackers. Stuff that matters.
Re:Does C++ still deserve a bad rap? by flowerlad on Friday October 16, 2020 @08:17PM (#24806307)
C++ is a very poorly designed language. A lot of the work being done by the committees is fixing Stroustrup's mistakes.

Consider horrors such as this. In the following statement what are foo and bar?

   int x = foo(2) + bar(3);
Most people will say foo and bar are functions and that's a reasonable guess. Unless you are talking about C++. In C++ foo and bar could be functions but could also lots of other things. For example it could be the equivalent of the statement below:

   int x = (foo)2 + (bar)3;
In other words type casting can be written as (foo)2 or as foo(2). Just to make it harder to understand what is going on. As another example consider implicit type conversions:

   Foo b = c + 3;
If 3 is not compatible with whatever type c is then C++ checks if there is a constructor that accepts an int parameter, and it automatically calls the constructor to convert 3 to the appropriate type.

Many of these craziness is being addressed by standards committees. C++ as designed by Stroustrup is a horrible language. Just look at his C++ book. It is huge and heavy and in fact Stroustrup takes pride in how big and complex the language is. C is an awesome language. C++ is horrible.

Re:Does C++ still deserve a bad rap? by beyondCritics on Friday October 16, 2020 @09:23PM (#24806718)
This story about the committees fixing Stroustrup's "mistakes" is completely made up from you and is not even remotely true. Also Stroustrup does _not_ take pride in how big the language is, the exact opposite is true. E.g. you will never ever hear Stroustrup speaking about template meta programming or similar weird stuff. He always tries to teach the practical side of C++. His C++ book is not big because the language is complex, but because it is a genral introduction into programming with C++. In fact, if you want to learn the esoteric features of C++, you have to buy other books. Implicit type conversions are inherited from C, the language you called "awesome". Many arcane features of C are nowadays considered as anachronisms and no modern designed language would repeat them.
Re:Does C++ still deserve a bad rap? by flowerlad on Friday October 16, 2020 @09:59PM (#24806884)
> Implicit type conversions are inherited from C, the language you called "awesome".

It is true that C language has some very limited implicit type conversions. For example you can say:

   long n = 3;
And the integer 3 is converted to long. This is extremely limited, and does not make the program hard to understand, which is completely different from the craziness you see in C++.
Re:Does C++ still deserve a bad rap? by beyondCritics on Friday October 16, 2020 @10:54PM (#24807105)
C does destructive conversions automatically, if you ask for it. E.g.

  unsigned n;
  n = 2.99;      // n = 2
  n = -1;        // n = 2^M-1;
  n = 1.0E33;    // undefined but not catched
Both C and C++ follow the same philosopy: "Trust the programmer". The programmer is expected to use his expressive freedom, to solve performance problems and stay away from problematic constructs without being told to so. C is a systems programming language from the 70's, designed to solve performance problems and C++ inherits from that. It is true, that in C++, you can overload assignment, addition and throw exceptions everywhere, thus making even simple expressions like

  auto n = a + b;
completely unpredictable. But the coder simply has abused the freedom C++ gave him. T some extent, the same is possible in C, look here https://www.ioccc.org/
Re:Does C++ still deserve a bad rap? by beyondCritics on Friday October 16, 2020 @11:16PM (#24807191)
Either you love C++ or you hate it. To tell the truth, i am also of the opinion that C++ is fucking crazy complicated. There are youtube videos from leading experts, explaining the usage of simple language elements at length, something like that is unheard of in modern languages. E.g. you are not expected to grasp overload resolution to the fullest extent: https://en.cppreference.com/w/cpp/language/overload_resoluti... Mastery takes likely years of experience.
Re:Does C++ still deserve a bad rap? by pooya13 on Saturday October 17, 2020 @04:32AM (#24808443)
> Both C and C++ follow the same philosopy: "Trust the programmer"

I disagree. I think ever since it’s inception from C, C++ has tried to increase type safety and continuously move more of the work to the compiler (latest example is concepts). It does empower the programmer to do what they want: OOP, FP, GP...

The philosophy of the language is mostly about abstractions. Abstracting objects, types, resource management etc And using modern C++ features and a recent compiler makes it harder to make mistakes. Using some generally accepted guidelines and static analysis tools helps even further.

Though the syntax is at times ugly due to the age of the language and it has a lot of inertia that makes it hard to get rid of some bad design decisions like some of the defaults, unless someone recreates c++ with the same flexibility and power, and the same powerful compilers/tools but with better syntax and defaults I don’t see the language going anywhere. And I am not holding my breath for another language to quickly be able to reproduce the C++ echo system that have taken decades to develop.

Re:Does C++ still deserve a bad rap? by srg0 on Saturday October 17, 2020 @02:13PM (#24811929)
The philosophy of C++ is about _zero cost_ abstractions.
Re:Does C++ still deserve a bad rap? by fbkr on Saturday October 17, 2020 @03:14AM (#24808062)
> C language has some very limited implicit type conversions

What? C will happily compile this:

    void g() {
        float f = 3.14;
        int* ip = &f;
    }
C++ has no such _craziness_. Your strawman actually has pretty good uses:

    complex c = 3i;
    ... = c + 4;
Would you rather have the last line not compile because 4 is not a complex number? Because one _could_ argue that 4 is a complex number, and C++ can represent this with an implicit constructor.

The issue with C++ is that implicit is the default, not that it exists.

Re:Does C++ still deserve a bad rap? by 59nadir on Saturday October 17, 2020 @08:17AM (#24809418)
One of the main gripes I have with C++ is exactly what you said, that the defaults usually are the wrong for the usual cases and for general sanity.

With that said, C programmers also seem to love their implicit behavior so it's pretty clear where this kind of thinking comes from.

Re:Does C++ still deserve a bad rap? by cygx on Saturday October 17, 2020 @08:51AM (#24809583)
C will happily compile this

Only if you ignore your compiler output: Even TinyCC will warn about that one on default settings. With the flags I use, the code would in fact fail to compile.

Re:Does C++ still deserve a bad rap? by moonchild on Saturday October 17, 2020 @05:24AM (#24808742)
The second case is easily solved by having an overload operator+(complex lhs, int rhs). No need to convert anything.
Re:Does C++ still deserve a bad rap? by fbkr on Saturday October 17, 2020 @04:28PM (#24813023)
This fails when you want `4 + c` or `c + 4.f` etc. So you either have a gross number of overloads doing the same thing, or you just put an implicit constructor to `complex`.

Also, I agree that implicit conversions in general should be avoided, but this complex number example feels like the perfect use case.

Re:Does C++ still deserve a bad rap? by brianush1 on Saturday October 17, 2020 @07:58AM (#24809343)

    complex c = 4;
Would you rather have this line not compile because 4 is not a complex number? Because one _could_ argue that 4 is a complex number, and C++ can represent this with an implicit constructor.

The issue with C++ is that implicit is the default, not that it exists.

Re:Does C++ still deserve a bad rap? by eru on Saturday October 17, 2020 @09:34AM (#24809861)
Haskell solves this reasonably well.

If your data type implements the Num typeclass, you can use literals like 4. (A typeclass in Haskell is similar to what they call an interface in Java.)

There's no automatic conversion happening at all. It's done via overloading literals at compile time. (You can do the same for strings.)

Re:Does C++ still deserve a bad rap? by srg0 on Saturday October 17, 2020 @02:35PM (#24812103)
C++20 introduced concepts, and now it's possible to specify type constraints, like integral, in a way similar to Haskell typeclasses.

What it doesn't solve that by default the compiler tries to find an appropriate implicit conversion. Sometimes it's convenient, sometimes it's harder to see what the code actually does.

Re:Does C++ still deserve a bad rap? by eru on Sunday October 18, 2020 @06:38AM (#24816844)
Yes, concepts help a bit, but I'm not sure whether they would be necessary here.

Yes, the implicit conversion for all values is part of the problem. The Haskell approach only gives you magic for literals.

Re:Does C++ still deserve a bad rap? by pmoriarty on Friday October 16, 2020 @08:32PM (#24806426)
"Most people will say foo and bar are functions and that's a reasonable guess. Unless you are talking about C++. In C++ foo and bar could be functions but could also lots of other things."

Same as in Lisp, one of the most beloved of languages on HN.

Were Lisp's macros a mistake? Some will say yes, but overall the language is more praised for them than damned.

So highly regarded are Lisp macros that many languages will eagerly proclaim that they have macros too -- even if they're not homoiconic and so harder to work with than in Lisp. Few if any are the languages that proudly proclaim that they don't have macros because they thought Lisp macros were a big mistake.

C++ fans themselves often point out that C++ templates can be as powerful as Lisp macros.

So which is it? Is giving users the power to radically transform their language a bug or a feature?

Re:Does C++ still deserve a bad rap? by jnxx on Saturday October 17, 2020 @06:46AM (#24809054)
This is perhaps a misunderstanding. Lisp macros are a part of the language. Of course you can use them to make your code unreadable. But what they are used for is to evolve and add definitions for things such as iteration, which in other languages, like Python or C++ is not possible without a language change.

For example:

https://lispcookbook.github.io/cl-cookbook/iteration.html

https://docs.racket-lang.org/reference/for.html

Re:Does C++ still deserve a bad rap? by ajkjk on Friday October 16, 2020 @09:01PM (#24806613)
I don't think lisp is that beloved here. It's just that the few people who love it love to bring it up.
Re:Does C++ still deserve a bad rap? by saagarjha on Saturday October 17, 2020 @05:07AM (#24808660)
Hacker News is written in a Lisp, started by people deeply interested in the language, and frequented in a large part by people who feel similarly.
Re:Does C++ still deserve a bad rap? by ajkjk on Saturday October 17, 2020 @10:07PM (#24815023)
I think that last clause is incorrect. It might have once been correct but I suspect most people on here have never used Lisp at all.
Re:Does C++ still deserve a bad rap? by giomasce on Saturday October 17, 2020 @02:38AM (#24807885)
Well, constructors are not very different from functions, from the caller point of view. Also, Python is much worse in this: any call can be monkey patched at basically any point during the execution, but people do not (usually) use this as an example of poor language designing. You expect the programmer to use the language features considerately.

BTW, in C++ every call is resolved at compile time, and the compiler could tell you what you are exactly doing at each line, including whether your examples are function calls or casts. If you dump the right table in GCC the information is there, although it's a pity there is no convenient way to access it. Cppexplorer helps a little bit, but I always find it difficult to understand. It's a problem of tooling, though, the language is well defined.

Re:Does C++ still deserve a bad rap? by formerly_proven on Saturday October 17, 2020 @06:31AM (#24809010)
> If you dump the right table in GCC the information is there, although it's a pity there is no convenient way to access it.

CLion, ctrl+B.

Re:Does C++ still deserve a bad rap? by john_moscow on Friday October 16, 2020 @09:27PM (#24806737)
>In other words type casting can be written as (foo)2 or as foo(2). Just to make it harder to understand what is going on.

Or to make it easy to describe high-level behavior where you don't care about the details. In your example, foo and bar could be some complex vector or matrix types with custom allocation/deallocation logic.

So this line:

  int x = foo(2) + bar(3);
Is an equivalent of 7 lines in Plain C:

  foo tmp_foo;
  bar tmp_bar;
  construct_foo_from_int(&tmp_foo, 2);
  construct_bar_from_int(&tmp_bar, 3);
  int x = add_foo_and_bar(&tmp_foo, &tmp_bar);
  free_foo(&tmp_foo);
  free_bar(&tmp_bar);
Sure, if you don't know what foo and bar are, it's confusing. But if you want to decompose what you are doing from how you are doing it, this syntax rocks.

BTW, (foo)2 syntax is preserved for backward compatibility with C and is deprecated in favor of static/dynamic/reinterpret_cast<>() (that many people don't use due to clunkiness) and foo(2) is unavoidable if foo's constructor has more than 1 argument.

Re:Does C++ still deserve a bad rap? by api on Friday October 16, 2020 @08:27PM (#24806388)
This weird ambiguous syntax exists to allow among other things templates that take either primitive or class types. It makes primitive types usable as if they are classes with copy constructors.

That being said, it can certainly be ugly. A lot of the ugliness comes from trying to maintain itself as a mostly superset of C. Technically it no longer is strictly that, but most C code will compile fine in a C++ source file.

Re:Does C++ still deserve a bad rap? by barumi on Saturday October 17, 2020 @03:00AM (#24807989)
> Consider horrors such as this. In the following statement what are foo and bar?

I don't understand your point. What's exactly the problem of knowing what are foo and bar? Even if you struggle with C++, any half decent IDE allows you to check the symbol and see what's being called with a single mouse click.

So what's exactly the problem?

> As another example consider implicit type conversions:

Again, what's exactly the problem? If you write down "c+3" it's your responsibility to know what you are doing and that "c+3" does make sense. And your example can only start to become a possibility if you explicitly and quite intentionally define your constructors to allow implicit type conversions, which is a direct violation of basic C++ best practices.

Adding to that, your convoluted example is no where near the mess that's javascript or Python and somehow people manage to not screw that up even without any type checking.

So why single out C++ although it does provide all sorts of guardrails and sanity checks?

> Many of these craziness

You failed to show any semblance of "craziness" in any of the convoluted examples you presented. In fact, you only showed you struggle with a language you barely have any experience with, let alone a grasp on rudimentary best practices such as requiring explicit constructors. Thus, why pin the blame on the language instead of where it really belongs: your lack of knowledge and experience using the language?

Re:Does C++ still deserve a bad rap? by 29athrowaway on Friday October 16, 2020 @08:57PM (#24806594)
If you mark the constructor as explicit, the automatic cast you mention does not take place and you'll get a compile time error.

The casting style can be enforced via linting. There are also C++ cast operators, that in my opinion should be preferred to the C style casts: static_cast, dynamic_cast, reinterpret_cast, const_cast, static_pointer, dynamic_pointer_cast, const_pointer_cast... and you can enforce that those are preferred rather than C style casts.

Re:Does C++ still deserve a bad rap? by jeffbee on Friday October 16, 2020 @09:23PM (#24806721)
I mean, it's even more weird than you said. It's not impossible that Foo(int) and (int)Foo or int g = foo do completely different things. Foo could define an `operator int` that terminates the program, or does any damn thing.
Re:Does C++ still deserve a bad rap? by gpderetta on Saturday October 17, 2020 @08:57AM (#24809614)
Thanks to macros foo(2) can be litterally anything in awesome C.