News for hackers. Stuff that matters.
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.