News for hackers. Stuff that matters.
Re:C/C++ vs. Rust Performance by identity0 on Sunday November 01, 2020 @12:48PM (#24961125)
> Secondly, the killing feature of C++ is that it is C. If you don't want to use exceptions or RTTI, then you can just switch the features off. Most of C programs can be just compiled with a C++ compiler with very small changes or without any changes at all.

If your C code happens to be compileable with a C++ compiler, then either your program is very small or you are writing really shitty C.

Re:C/C++ vs. Rust Performance by MaxBarraclough on Sunday November 01, 2020 @01:19PM (#24961331)
Why's that? C has a few features that C++ lacks, but it's possible to get by fine without using any of them.

The Lua interpreter is written in a subset of C so that it also compiles fine as C++. Or, if you prefer, in a subset of C++ so that it compiles fine as C.

https://www.lua.org/pil/24.1.html

Re:C/C++ vs. Rust Performance by josefx on Sunday November 01, 2020 @01:30PM (#24961401)
Something as simple as malloc requires a cast in C++ when it is not needed in C, so type errors for even simple code.
Re:C/C++ vs. Rust Performance by MaxBarraclough on Sunday November 01, 2020 @01:52PM (#24961542)
What type errors? Do you mean that, to get it to compile as C++, you have to add an explicit cast? That isn't a significant burden.

Requiring explicit casts, and forbidding implicit conversions, reduces the odds of being surprised by an unwanted conversion. There's a good argument to be made for adding explicit casts to C code even when the language doesn't require them. GCC's -Wconversion flag can help with this.

Re:C/C++ vs. Rust Performance by josefx on Sunday November 01, 2020 @02:22PM (#24961783)
> What type errors? Do you mean that, to get it to compile as C++, you have to add an explicit cast?

Yes.

> That isn't a significant burden.

But once you do it your "C" code is littered with a lot of unnecessary casts that increase the verbosity of the code and could hide errors. With pre C99 code it would also hide default int errors if the include for malloc gets lost at some point.

Re:C/C++ vs. Rust Performance by MaxBarraclough on Sunday November 01, 2020 @02:36PM (#24961881)
> and could hide errors

How? As I said, the stricter rules of C++ result in fewer type-conversion errors than C's permissive rules. That's the reason for C++'s stricter rules, and it's the reason Ada is even stricter.

As an example, consider

    a = b = c;
In a languages with permissive rules about implicit conversions, you have to inspect the variables' types to determine whether this statement is equivalent to

    b = c;
    a = c;
(Even ignoring OOP, that is.)

> With pre C99 code it would also hide default int errors if the include for malloc gets lost at some point.

Wouldn't it just fail to compile if you're missing a required header?

edit steveklabnik linked to [0] so I see what you mean now. I think that's a valid point.

[0] https://stackoverflow.com/a/605858/

Re:C/C++ vs. Rust Performance by ByteJockey on Sunday November 01, 2020 @02:04PM (#24961643)
But the conversion of the result of a malloc call is neither surprising nor unwanted.

There's also no ambiguity. You've already explicitly defined the variable's type at that point.

Re:C/C++ vs. Rust Performance by MaxBarraclough on Sunday November 01, 2020 @02:21PM (#24961772)
There's very little harm in making the conversion explicit. It's more verbose, but it's also more explicit, which is a good thing.

There is harm in implicit conversions catching the programmer unawares. This is a fairly significant cause of bugs in C code, and it's why C++ uses stricter rules, requiring you to always make these conversions explicit, even when using malloc.

Re:C/C++ vs. Rust Performance by steveklabnik on Sunday November 01, 2020 @02:24PM (#24961796)
There is a significant group of folks that believe it is actively bad. I don't write enough C these days to have a stance, personally, but https://stackoverflow.com/questions/605845/do-i-cast-the-res...
Re:C/C++ vs. Rust Performance by ncmncm on Sunday November 01, 2020 @10:34PM (#24964605)
There is a significant group who are wrong about literally anything one can think of. How vocal they are about it does not correlate with much.
Re:C/C++ vs. Rust Performance by ByteJockey on Sunday November 01, 2020 @10:45PM (#24964666)
But that fact does not get us any further towards figuring out who is wrong. It merely tells us that people disagree.
Re:C/C++ vs. Rust Performance by ncmncm on Sunday November 01, 2020 @11:13PM (#24964773)
People still coding C can usually be counted on to be wrong.
Re:C/C++ vs. Rust Performance by ByteJockey on Sunday November 01, 2020 @10:55PM (#24964696)
You're not entirely wrong, but in the context of the specific example given (malloc), it's about as implicit as c++'s auto keyword, it just obscures type information on the right side instead of the left side of the assignment from the programmer.

I would also argue that there's a hard to quantify harm in making code more verbose. More visual noise makes it harder to parse what's going on. Sure, one redundant cast isn't going to kill you. But many sources of repetition across an entire code base eventually turn it into a slog where the parts you want are obscured by useless noise.

Re:C/C++ vs. Rust Performance by dathinab on Sunday November 01, 2020 @01:00PM (#24961213)
Idk about "shitty C" but indeed C++ being a super set of C is a common misconception, followed by the misconception that it's nearly a superset of C. ;=)

Simplest example is that a `void func()` decl in C and C++ have different semantic meanings.

Re:C/C++ vs. Rust Performance by simias on Sunday November 01, 2020 @01:50PM (#24961532)
I think "nearly a superset" is correct, the empty argument list doesn't really make enough of a difference to really make it a completely different language. I also don't think I've ever seen this "feature" of C used productively in modern codebases, except maybe to declare generic function pointers in something like dlfunc. In general it's just that people are too lazy to type `void func(void)` (or don't know the difference).

You can also tell that the C++ standard committee agrees with this to some extent since they tend to add C novelties into the C++ standards whenever possible to make it easier to write compatible code, for instance: https://en.wikipedia.org/wiki/C%2B%2B11#Improved_C_compatibi...

Re:C/C++ vs. Rust Performance by loeg on Sunday November 01, 2020 @02:14PM (#24961724)
Standard C++ still lacks C99 compound literals, doesn't it? That's the biggest gap I've run into. It is a common and practical construct in C.
Re:C/C++ vs. Rust Performance by jstimpfle on Sunday November 01, 2020 @03:01PM (#24962042)
And designated initializers.
Re:C/C++ vs. Rust Performance by ncmncm on Sunday November 01, 2020 @11:09PM (#24964757)
Yet, C++ supports designated initializers.
Re:C/C++ vs. Rust Performance by Gibbon1 on Sunday November 01, 2020 @02:04PM (#24961644)
I feel like treating C as a subset of C++ has done real harm to the language.
Re:C/C++ vs. Rust Performance by simias on Sunday November 01, 2020 @03:01PM (#24962043)
I agree that teaching C++ as "C and then some stuff on top" is probably a bad idea. But I also think that it's not technically incorrect to say that "C is mostly a subset of C++", at least as far as most C constructs will work in C++ with only minor tweaks.

It's a bit like saying that operating the windscreen wipers is a subset of knowing how to drive a car. It's strictly correct, but it's probably not where you should start nor where you should put most of your attention if you're looking to get you license.

Re:C/C++ vs. Rust Performance by rrdharan on Sunday November 01, 2020 @02:21PM (#24961780)
Which one?
Re:C/C++ vs. Rust Performance by Gibbon1 on Sunday November 01, 2020 @04:32PM (#24962683)
No arrays, no real strings. Pushing UB issues on the programmer instead of forcing the compiler maintainers to deal. No closures. Threading as a first class construct. No modules. Standard Library is increasingly a bad joke.

What's happened is the direct opposite of C++ where every fad gets added to the language. With C lots of standard proven features don't make it into the language.

Re:C/C++ vs. Rust Performance by rrdharan on Sunday November 01, 2020 @07:33PM (#24963856)
Heh, yeah my point was you could plausibly apply your statement to both languages, i.e. their confusing coupling impedes both in different ways.
Re:C/C++ vs. Rust Performance by attractivechaos on Sunday November 01, 2020 @02:55PM (#24962003)
> If your C code happens to be compileable with a C++ compiler, then either your program is very small or you are writing really shitty C.

It is easy to write C code that is compatible with C++ if you have compatibility in mind from the beginning. Most of time: 1) cast malloc() – you can define macros for less code; 2) avoid C++ keywords; 3) don't use VLA which is not recommended anyway; 4) add __STDC_LIMIT_MACROS for uint64_t etc. Use -Wc++-compat also helps. It is harder to modify existing C code for the compatibility with C++.

Re:C/C++ vs. Rust Performance by krizhanovsky on Sunday November 01, 2020 @01:44PM (#24961488)
Well, we didn't mention this in the article, but this is very practical actually. About 10 years ago we had a request significant reworking an open source DNS server and the main problem with the project was that almost whole logic was placed in about 10 functions and most of the functions exceeded 1000 LoC. Each code update involved plenty of headache with dynamic memory freeing. We spent about 1-2 days to compile it with C++ and replace the most crucial pointers with smart pointers, that we didn't have to care about memory freeing. That time we had to make the job done ASAP, surely we should have spend time in proper code refactoring.

Surely you need to modify C code to compile it with C++ and in the article we made an example of the modification. But the point is that the changes are pretty straightforward and small.

Re:C/C++ vs. Rust Performance by the_mitsuhiko on Sunday November 01, 2020 @02:36PM (#24961883)
I wouldn’t want to be tasked with maintaining a C++ codebase like that. That sounds horrible.
Re:C/C++ vs. Rust Performance by krizhanovsky on Sunday November 01, 2020 @01:46PM (#24961499)
Also if you don't like my example with quick fix of some dirty project, then consider InnoDB storage engine which was developed in pure C for 2 decades, but now it's C++.
Re:C/C++ vs. Rust Performance by pengaru on Sunday November 01, 2020 @03:16PM (#24962124)
> If your C code happens to be compileable with a C++ compiler, then either your program is very small or you are writing really shitty C.

And yet the standard practice for games written in C attempting to use Valve's C++ API for Steamworks integration is to switch to using a C++ compiler.

Re:C/C++ vs. Rust Performance by GirkovArpa on Sunday November 01, 2020 @04:52PM (#24962848)
I pasted the contents of this(1) ~100 line C file into this(2) C++ file with no problems.

[1] https://github.com/nospaceships/raw-socket-sniffer/blob/mast...

[2] https://github.com/GirkovArpa/raw-socket-sniffer/blob/master...

Re:C/C++ vs. Rust Performance by simias on Sunday November 01, 2020 @01:42PM (#24961475)
I see what you mean but I think the point the article is making here is true: you can effectively strip most of the features of C++ and end up with something with a footprint and runtime cost almost identical to C. You can effectively port almost any C program to C++ by making purely syntactical changes that won't have any impact on the execution.
Re:C/C++ vs. Rust Performance by b20000 on Sunday November 01, 2020 @01:29PM (#24961393)
the linux kernel is written in C ...
Re:C/C++ vs. Rust Performance by wtetzner on Sunday November 01, 2020 @01:45PM (#24961496)
And it’s likely not compatible with a C++ compiler.
Re:C/C++ vs. Rust Performance by Subsentient on Sunday November 01, 2020 @01:50PM (#24961528)
It definitely isn't. C99 and even some C11 features used all over the place. Designated initializers are heavily used. We'll see when those actually end up stable for C++20.

Even then, I never use the latest standards of C++, I stay a couple standards behind so I can rely on whatever compiler I pick being able to handle it. Right now I'm using C++14.

Re:C/C++ vs. Rust Performance by b20000 on Monday November 02, 2020 @01:44PM (#24971513)
downvoted by the rust fanboys