News for hackers. Stuff that matters.
Re:Modern C for C++ Peeps by moloch-hai on Saturday January 07, 2023 @10:48AM (#34289102)
Yeah, stopped reading after that. How wrong does he want to be?

It is hard to tell what the point is of this article is. Yes, the C-ish subset of C++ differs from ISO C, in trivial details. That is irrelevant to C++ users, who may consult ISO C++ to discover the actual subset. They interact with any C only when they #include a header for a 3rd party C library, and then the relevant subset is what, exactly, appears in that header.

There is really no legitimate reason to code C anymore: A C++ compiler is available for any modern development target. Confining yourself to any C subset (with or without ISO C marginalia) amounts to crawling when you can fly.

To be more specific: nothing that can be done using C is unavailable to the C++ coder, but enormous power is available to the C++ coder and wholly unavailable in C.

Re:Modern C for C++ Peeps by saghm on Saturday January 07, 2023 @11:41AM (#34289718)
> There is really no legitimate reason to code C anymore: A C++ compiler is available for any modern development target. Confining yourself to any C subset (with or without ISO C marginalia) amounts to crawling when you can fly. > > To be more specific: nothing that can be done using C is unavailable to the C++ coder, but enormous power is available to the C++ coder and wholly unavailable in C.

As a counterargument, sometimes constraints can be valuable in their own right rather than just being requirements for some larger goal. The most prominent example of this in programming design is type checking; enforcing type checking at compile time strictly reduces the number of programs you can express, so dynamic languages can do everything that static languages and more, and yet it turns out that it's still pretty useful specifically because we sometimes want to avoid the programs that a dynamic language would allow but a static language would reject. At the risk of getting too philosophical, imagine framing programming as an exercise in trying to write rules to specify one program out of the set of all possible programs, where each bug you write that changes the semantics moves one step further from your goal. The compiler can act as a "filter" on the set of programs you can potentially select by refusing to process your rules if they would specify one of the forbidden programs, which reduces the chance selecting the wrong program by mistake. In this scenario, the theoretically ideal compiler would be the one that rejects all programs other than the exact one you want! This obviously isn't possible in the general case, but the point here is that as long as compilers don't reject the exact program you want, a compiler that rejects more programs is _more_ useful.

I'm not making any claim about whether it is in fact worth it to code C anymore, but I disagree with the logic that C++ letting you do anything C can do and more is proof that C++ should always be used over C. Maybe C is crawling compared to C++'s flying, but if my goal is to get a pen that falls under my desk, it's a lot more useful for me to not use an airplane to do it.

Re:Modern C for C++ Peeps by moloch-hai on Saturday January 07, 2023 @11:53AM (#34289879)
Getting your pen doesn't demand enormous power, but you are not obliged to field any, either way.

We generally don't know, when we set out, how much support we will be able to benefit from, down the road.

Re:Modern C for C++ Peeps by galangalalgol on Saturday January 07, 2023 @01:54PM (#34291285)
I think the gp was talking about anti-features. Austral's announcement included a list of features it avoided on purpose. Including several from c++. operator overloading, increment operators and even implicit oder of operations were listed as features that made codebases worse, even if no one has to use them.
Re:Modern C for C++ Peeps by moloch-hai on Saturday January 07, 2023 @03:21PM (#34292299)
Coding by superstition was a bad idea before Austral, and remains a bad idea after.
Re:Modern C for C++ Peeps by jstimpfle on Saturday January 07, 2023 @10:55AM (#34289206)
For me the legitimate reason to code C is to get stuff done, period.

There is hardly a need for more (to do what I do -- systems in the broadest sense) and there's a lot of time saved by not thinking about 21 "safe" and "ergonomic" ways to write everything in C++ (that very often end up being not that safe, and unintelligible).

Yes, there is the "subset" argument, only use what you need. But I don't buy that, I'm not like that, it doesn't work for me. Constraining myself makes things easier.

Re:Modern C for C++ Peeps by glowingly on Saturday January 07, 2023 @11:04AM (#34289303)
This somewhat mirrors my own development.

I started in C, and am still in it for systems/OS development at work.

In UI development, we had older C toolkits, but are replacing them with newer toolkits that are C++. I started off trying to just do C business logic with some C++ glue code for the UI. Sticking to what's familiar. In the end, it was just easier to do everything properly. All C in areas that are C. All C++ in areas that are C++. Same with C#. Trying to mix them is just more work than necessary.

At least I get to add C++ to my resume. There is a little joke at work, w.r.t. our existing codebases and code choices. They are career-driven choices, not technical choices. The result is a mess of a codebase, but I'm fully embracing that now.

Re:Modern C for C++ Peeps by RcouF1uZ4gsC on Saturday January 07, 2023 @11:31AM (#34289601)
> For me the legitimate reason to code C is to get stuff done, period.

If I want to just get stuff done, C++ is great for that. Don’t underestimate how much you gain by having string, vector, and map when you just want to crank out code.

Nobody is forcing you to overdesign stuff. You can always just crank out the code you want.

Re:Modern C for C++ Peeps by dkersten on Saturday January 07, 2023 @12:41PM (#34290476)
Yeah, just having the standard library container types (or similar generic third party libraries if you don’t like std ones, eg I tend to use phmap maps and sets instead of std::unordered_map/set). Templated containers just make using them so much easier than generic containers in C. Ergonomics matter when you just want to get things done. Plus the C++ containers make it easier to manage memory IMHO.

The main reason I still sometimes use C is compiler support for microcontrollers where everything tends to have a C compiler but not everything has a C++ one (or if it does, not necessarily an up to date one).

I don’t use either in my day to day really but I do have sone personal projects. Ultimately I foresee doing these in Rust where I can, but for stuff like hobbyist game development, I’m currently too bought into some of the C++ libraries, primarily EnTT, but even there I think I’ll eventually just end up using Rust with Bevy. For now though, I actually quite enjoy using C++17 (and maybe 20 soon if I find the free time).

Re:Modern C for C++ Peeps by pjmlp on Saturday January 07, 2023 @02:14PM (#34291522)
Even something like Turbo C++ for MS-DOS is preferable to raw C, the only embedded toolchains that don't offer even that are usually stuff like PIC and similar.

Which, in any case, have companies like Mikroe selling Basic and Pascal compilers.

Re:Modern C for C++ Peeps by tejohnso on Saturday January 07, 2023 @12:08PM (#34290078)
> There is really no legitimate reason to code C anymore

That's an interesting statement. Reminds me of statements indicating any new code base should be started in rust over c++.

Re:Modern C for C++ Peeps by pjmlp on Saturday January 07, 2023 @12:27PM (#34290309)
The rewrite in C++ movement was lived in Usenet flamewars, and we were in good path with all the userspace C++ frameworks in OS/2, MS-DOS, Windows, Apple and BeOS until the GNU Manifesto came around and urged everyone to write FOSS code in C.

"Using a language other than C is like using a non-standard feature: it will cause trouble for users. Even if GCC supports the other language, users may find it inconvenient to have to install the compiler for that other language in order to build your program. So please write in C."

GNU Coding Standard in 1994, http://web.mit.edu/gnu/doc/html/standards_7.html#SEC12

Re:Modern C for C++ Peeps by moloch-hai on Saturday January 07, 2023 @01:21PM (#34290892)
Stuff written about C++ before there was an ISO Standard obviously does not apply after. And, we should know by now how much Richard Stallman's prejudices are worth.
Re:Modern C for C++ Peeps by pjmlp on Saturday January 07, 2023 @02:01PM (#34291366)
By all means, GNU Coding standards from 2006, 8 years after C++98.

"When you want to use a language that gets compiled and runs at high speed, the best language to use is C. Using another language is like using a non-standard feature: it will cause trouble for users. Even if GCC supports the other language, users may find it inconvenient to have to install the compiler for that other language in order to build your program. For example, if you write your program in C++, people will have to install the GNU C++ compiler in order to compile your program.

C has one other advantage over C++ and other compiled languages: more people know C, so more people will find it easy to read and modify the program if it is written in C.

So in general it is much better to use C, rather than the comparable alternatives."

http://gnu.ist.utl.pt/prep/standards/html_node/Source-Langua...

As project lead, his opinions always mattered for what goes into GNU.

What the rustaceans are doing nowadays I lived as C++ fanboy, so I am pretty aware how things went.

Remember that besides the license issue, another big reason why GNOME came to be was because KDE uses C++, hence the whole GObject stuff in C.

Re:Modern C for C++ Peeps by moloch-hai on Saturday January 07, 2023 @02:13PM (#34291516)
You emphasize my point so clearly, I need add nothing.
Re:Modern C for C++ Peeps by pjmlp on Saturday January 07, 2023 @03:31PM (#34292412)
That is now, we were talking about when GNU/Linux started to be relevant and what triggered language choice, versus what was happening in the desktop PC world.

The latest version of the GNU coding standard is much more permissive.

Re:Modern C for C++ Peeps by zozbot234 on Saturday January 07, 2023 @07:17PM (#34294584)
Yes but back then you could compile C with a C++ compiler and get the same type checking as in actual C++. There were a few pitfalls but a shared "Clean C" coding style was viable and that's what most of the rewriting effort was focused on.

C++ did add stronger modularity with its public and private member specifiers, namespacing etc. but that was mostly useful on larger projects.

Re:Modern C for C++ Peeps by moloch-hai on Saturday January 07, 2023 @01:19PM (#34290869)
Rust can still fizzle, like Ruby has.

It is still far from clear whether anybody will be learning Rust, in ten years, instead of whatever is the new hotness then. Hiring a domain expert who also knows Rust is generally impossible today, and will be for, at least, a long time. So, starting a new project in Rust is OK for a project you know won't matter, but absurdly risky for one that will.

C++ is mature. None of the above is a concern, for C++.

Re:Modern C for C++ Peeps by AgentOrange1234 on Saturday January 07, 2023 @02:48PM (#34291890)
Hrmn. Hiring rust experts may be hard, but the opportunity to learn rust on the job could be appealing to a lot of devs who want to grow?
Re:Modern C for C++ Peeps by moloch-hai on Sunday January 08, 2023 @12:45AM (#34296436)
Domain experts are focused on their domain. Becoming a beginner again is a recipe for radically reduced productivity. The people eager to learn a new language on the job are generally those who have not invested much in anything.
Re:Modern C for C++ Peeps by AgentOrange1234 on Sunday January 08, 2023 @09:13AM (#34299235)
It’s not like rust is some radically different thing. Getting productive with it should take a good developer what, a couple of months?
Re:Modern C for C++ Peeps by int_19h on Saturday January 07, 2023 @05:04PM (#34293403)
In terms of adoption by pre-existing large players in the industry, Rust is already ahead of where Ruby was at its hype peak. It's just not quite as shiny because that code is not running some web app that is immediately demo-able, but some boring OS infrastructure stuff.

Given the money already invested into internal tooling and infrastructure specifically for Rust, I just don't see those large companies suddenly dropping it. Regardless of how good the language itself is, the sunk cost alone makes it hard to turn ship. And the language is good, so it'll use that time to entrench itself further.

Re:Modern C for C++ Peeps by moloch-hai on Sunday January 08, 2023 @12:54AM (#34296474)
They won't drop the language: whatever products depend on it will continue to depend on it. Unless hiring gets difficult; then they will transcribe it to a more mainstream language. In the meantime, language choice for new projects will be driven by experience hiring into these and other projects.

Other, newer languages will come up continually, siphoning off the most mobile who have begun to find Rust familiar and pedestrian.

Re:Modern C for C++ Peeps by phoehne on Saturday January 07, 2023 @12:41PM (#34290488)
Anyone who says C is not important hasn't worked on embedded systems or operating systems.

I work on embedded systems. There are roughly 3 choices for languages[1]. C, Ada, and C++. Except the version of C++ is sometimes often vendor specific and may lack features that are "standard C++", and never have a "full" STL. In addition, if you're not super careful with C++, what you thought was a copy or declaration can execute additional constructor or assignment logic. It might just interfere with your estimate of how long something can take (which is super important in interrupt handlers), or worst case, smash your stack (which can be as small as 1-2k). With C, you pretty much know what's going to happen and what's getting called. The fact that C doesn't require a runtime (nor does Ada), means you can use it to write kernels. You can't do the same with a 'full' version of C++, so you're back to a cut down C++.

I also spend time debugging compiled, optimized code by tracing instructions. This is hard enough in C, where I don't magically jump to a constructor function or an assignment function. I can visualize in my head how the C code could look like, given the instructions the optimizer produced. So it may not look like my code, but it is a version of the code that I can at least logically infer.

Ada is nice, but outside of some super-safety critical realms, it hasn't had the uptake. Private industry prefers not having to make the investment and the government let everyone waive out of it.

1. I know people are going to to say 'what about Rust, Go, or embedded something or other?' They are not as mature in their development lifecycle. So there's no validated RTOS for Rust (there are some experimental ones). Go is even further behind. Basically, you need a vendor supported RTOS that's validated for the safety and operational requirements of your use case, before you can be a serious choice for a lot of embedded work.

Re:Modern C for C++ Peeps by groos on Saturday January 07, 2023 @01:26PM (#34290958)
I'm not an Ada expert but I suspect any exception implementation would require a runtime of sorts.
Re:Modern C for C++ Peeps by phoehne on Saturday January 07, 2023 @01:56PM (#34291312)
It's been decades since I worked with Ada, so I could be wrong, but you could build it with an embedded runtime that allows you to do tasking and (I think I remember) exceptions. But it also has a language standard way to create limit certain features. The difference is the C++ is a grab bag, depending on the vendor, platform, RTOS, etc.
Re:Modern C for C++ Peeps by moloch-hai on Saturday January 07, 2023 @01:10PM (#34290788)
If you are using vendors' proprietary compilers in place of Gcc, it is purely masochism.
Re:Modern C for C++ Peeps by phoehne on Saturday January 07, 2023 @01:38PM (#34291076)
Some vendors base their compilers on GCC or Clang. But if you're not using their provided compilers, you either need to 1) be willing to shoulder the expense of making changes to GCC or Clang for whatever you're adding to the silicon in terms of instructions, and 2) be willing to get your combination of RTOS and compiler re-validated for the safety or operational standard on which you need to deliver.

But even if you do use GCC or Clang, it doesn't change the mechanics of C++ as a poor language for embedded or operating system work. It just means you have the same choices (e.g. limited support for containers and strings, no exceptions, limited smart pointers, etc.) and you're making that choice based on GCC or Clang's limits.

Re:Modern C for C++ Peeps by moloch-hai on Saturday January 07, 2023 @01:49PM (#34291228)
Maybe you should explain to Andreas how C++ was such a bad choice for SerenityOS. Or to authors of the various RTOSes coded in it.
Re:Modern C for C++ Peeps by phoehne on Saturday January 07, 2023 @02:15PM (#34291541)
It's not impossible. Other people have done it. Some have done it with a minimal kernel or micro-kernel in C and then services on top of that in C++. Just doing a quick perusal of their code base, the low level stuff is functions and structs. So, yes, it is a .cpp but not that different from the code you'd write if it were a .c. And it appears they're turning off exceptions and the standard library (which is completely reasonable). They probably have some additional coding standard (internally) like you can't use anything with a constructor in certain contexts, etc.

And on a validated RTOS - it might be in C++ under the covers. The examples that come to my mind aren't. But the world is a big and magical place, so I can't say for certain on every RTOS. The F35 uses C++ and a set of very restrictive internal coding standards built on https://en.wikipedia.org/wiki/MISRA_C.

And no one will die if their copy of Serenity OS crashes.

Re:Modern C for C++ Peeps by pjmlp on Saturday January 07, 2023 @02:19PM (#34291571)
If it is compiled by a C++ compiler, it is C++ regardless of the language features being used.

Regarding language guides, C doesn't stop being C, even when castrated via MISRA-C.

Re:Modern C for C++ Peeps by Jtsummers on Saturday January 07, 2023 @01:15PM (#34290834)
Vendor compilers are common, if not typical, for embedded and safety-critical systems. It's not masochism since they work, and in my experience they work quickly to address any compiler bugs you may discover and report. This is also true for Ada, C++, and Fortran in that domain.
Re:Modern C for C++ Peeps by abrawill on Saturday January 07, 2023 @01:30PM (#34290993)
If the hardware is exotic I guess you’d have no choice. But for security critical don’t you run the risk of relying on obscurity rather than security due to the niche-ness of your stack?

What does a vendor compiler do or do better than a compatible generic one?

Re:Modern C for C++ Peeps by Jtsummers on Saturday January 07, 2023 @01:54PM (#34291295)
When you get a critical system certified for fielding you aren't just certifying the source code, but the actual executable and build process and test process and other things. This requires reproducibility for years to come. Choosing generic compilers may work in dev and parts of test, but not for actual deployment as a consequence (or it doesn't work well). Suppose you picked clang 11 several years back. Now you need to do an update to the system, you can still use clang 11, but not clang 14 at least not without doing a comprehensive recertification process. Also, if an issue is discovered in clang 11 it's likely been fixed in clang 14, but again you have to get your system recertified with clang 14. And that's if the issue is fixed, it may still exist.

With a vendor supplied compiler you can say, "We're using version 11.2". A year or two later an issue is discovered, the vendor will backport a fix to 11.2 giving you 11.2.1 which is much less effort for recertification. You aren't depending on the kindness of strangers (a terrible strategy) because you're actually paying someone to do the work.

Re:Modern C for C++ Peeps by abrawill on Sunday January 08, 2023 @02:45AM (#34297007)
That explains a lot! Never worked with certified code, but I see how it makes sense.

Do the vendor compilers have a very focused feature set as a result? To reduce both test surface area and chance of expensive mistakes?

Re:Modern C for C++ Peeps by moloch-hai on Sunday January 08, 2023 @01:00PM (#34301369)
Mainly their own costs. They have a captive market, and tooling is a "cost center".
Re:Modern C for C++ Peeps by pjmlp on Saturday January 07, 2023 @02:18PM (#34291564)
I love how usually not having support for full ISO C++ or it not being up to date is an issue, while having to deal with a cut down version of C, freestanding, a custom library and compiler extensions is a plus.
Re:Modern C for C++ Peeps by kllrnohj on Saturday January 07, 2023 @03:35PM (#34292460)
> Anyone who says C is not important hasn't worked on [..] operating systems.

Most OS code is written in C++ or Object-C, not C. Between Android, MacOS/iOS, and Windows there's not a lot of C code. The kernels are most of it. The NT kernel has C++, but hard to get a source on how much is still C or not. But the kernel is far from being most of the OS regardless.

> The fact that C doesn't require a runtime (nor does Ada), means you can use it to write kernels. You can't do the same with a 'full' version of C++, so you're back to a cut down C++.

For a huge number of users, "full" C++ is C++ with -fno-exceptions and -fno-rtti anyway, at which point the 'runtime' is like 2 functions and it's absolutely perfectly fine to use in a kernel. But regardless neither of those features are inherently incompatible with being in a kernel. You just have to implement a runtime to do that, just like kernels written in C have to implement a libc replacement.

Unless you're talking about the standard library (even though nearly all of it is completely kernel-compatible out of the box), but then you'd have to include libc & friends in the "C runtime" category and then it's equally impossible to use in a kernel.

Re:Modern C for C++ Peeps by phoehne on Saturday January 07, 2023 @04:59PM (#34293340)
Sorry, I didn’t mean the userland stuff above the kernel, which, along with the boot loader, is the part that is really restricted in any sense. With the standard library, there are large parts that are not kernel safe. That’s because they rely on services like memory allocation, which are different in the kernel. Or they rely on kernel services, like files. And what I work on has no kernel, just an RTOS.

And C in the kernel isn’t really a libc replacement. They often have different behaviors because they need to run without allocating memory, or be interrupt safe. For example, in some situations I use a function call instruction that specifically does not creat a new stack frame.

Re:Modern C for C++ Peeps by moloch-hai on Saturday January 07, 2023 @08:18PM (#34294976)
Very little of the standard C++ library is not "kernel safe". If a bit of it might need to allocate, you may provide it a kernel-grade allocator to use; but you probably use custom kernel containers, for other reasons. If you don't want any filebufs, you do not make them.

And of course asm {} works the same regardless.