3 ms·
Any established C codebase, for example the kernel or Postgres? Traditionally microcontroller firmwares as well, though those are increasingly friendly to C++,
by mikepurvis 1y ago
Any established C codebase, for example the kernel or Postgres?
Traditionally microcontroller firmwares as well, though those are increasingly friendly to C++, you just have to be careful about allocations as C++ makes it way easier to accidentally allocate than C does.
- neonz80 1y agoI'm not sure about other compilers, but compiling C code as C++ with MSVC ends up with pretty much the exact same code, instruction by instruction. C++ is a bit more strict though especially with casting, so a lot of code won't compile out of the box.
- vbezhenar 1y agoC++ code compiles to a different function names in object file (name mangling). You probably need to put a lot of ugly `#ifdef __cplusplus extern "C" {` boilerplate in your headers, otherwise C and C++ files will not compile together.
- winocm 1y agoDon't forget the infamous pattern used in some C projects too: struct foo decl = { .member = /* ... */ .next = &(struct nested_pointer) { .nested_member = /* ... */, }, .array = (struct nested_array[]) { [0] = { /* ... */ }, } }; This pattern does not work in C++ as the nested declarations become temporaries.
- charcircuit 1y agoNothing is stopping you from linking C++ code to Postgres.
- cryptonector 1y agoBut if you want to _contribute_ to PostgreSQL, it has to be in C.
- TuxSH 1y ago> Any established C codebase, for example the kernel or Postgres? Obviously you mean the Linux kernel, specifically. Also C++ has gotten a lot better since 2003 and before. Examples of C++ usage in commercial codebases: - anything Nintendo has written since 2010 (including microkernel, entire OS and all, for 3DS and Switch 1/2) - well-known, high performing databases like ScyllaDB > you just have to be careful about allocations as C++ makes it way easier to accidentally allocate than C does. With the exception of exceptions (and coroutines but that's easily customizable), it's merely a standard library thing and doesn't affect language features (incl. language features exposed as std:: funcs/traits) C++ has got a bad rep because it never fixes broken stdlib parts due to API and ABI concerns, and has many footguns due to this, that might make Rust and C better choices in corporate environments. However, IMHO, C++ is a much better language than C for personal projects, or when having a team of experienced folks.
- gpderetta 1y ago> Any established C codebase Anecdotally, GCC and GDB successfully (and incrementally) switched to C++ form C in the recent past. The Linux kernel will never do it for ideological reasons of course. I don't know about Postgres.
- cryptonector 1y ago> > Any established C codebase, for example the kernel or Postgres? > Obviously you mean the Linux kernel, specifically. Or any BSD, or Illumos, or Solaris, or any Unix-derived kernel, or... Even the Windows kernel is in C (or a very cut-down C++).