6 ms·
Why would you jump through all these hoops instead of just writing C++ if you want "C with generics"
by cherryteastain 1y ago
Why would you jump through all these hoops instead of just writing C++ if you want "C with generics"
- Kranar 1y agoBecause for many of the use cases where C is used, switching to C++ involves jumping through even more hoops.
- lionkor 1y agoDo you have a couple of real world examples?
- adastra22 1y agoEmbedded systems, for example.
- teraflop 1y agoI know it used to be, but is it really still common for embedded systems to use weird architectures that G++/Clang don't support?
- adastra22 1y agoUnless it is a popular system or common architecture, yes.
- jenadine 1y agoCould you show me an example of a micro controller still supported today which doesn't have a C++ compiler?
- foldr 1y agoThe 8051. I think C++ compilers technically exist for it, but for most hardware the only practical choice is the Keil C51 compiler, which is C89.
- jjmarr 1y ago> is it common for weird architectures to exist? > yes, unless you're using a common one.
- adastra22 1y agoThat isn’t a contradiction.
- pjmlp 1y agoOnly very weird ones that barely support C like PIC, most ones support at least C++98 or C++11.
- mikepurvis 1y agoAny 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.
- 1y ago
- rectang 1y agoWriting extensions for projects that support C extensions but may not support C++ extensions, e.g. many dynamic languages.
- Snarwin 1y agoYou can still write the extension in C++ and expose an extern "C" interface.
- rectang 1y agoThat's possible, but then the people building your extension need a C++ toolchain. The question was "please provide examples where switching to C++ involves jumping through even more hoops", and in my view requiring downstream to use a C++ environment when they're expecting to use a C environment qualifies.
- uecker 1y agoTrue. For me, C++ itself is the maze of hoops I would rather want to avoid.
- pjmlp 1y agoMost C compilers are C++ toolchain as well, we are no longer in the 1990's. Unless of course the project is using such a old compiler.
- rectang 1y agoProblems in this domain arise more because you’re wandering off the beaten path for configuration and tooling than because the systems lack absolute capabilities. If you don’t want to build your extension the way that the extension framework expects you, all sorts of annoyances show up. Maintaining an cross-platform compatible C extension is hard enough already.
- _proofs 1y agoliterally a good majority of existing embedded software coupled to applications in safety -- devices used by fire safety and first responders.
- deleted 1y ago[deleted]
- _proofs 1y agobecause i work on a legacy project that is coupled to safety regulations and other quality guarantees, and we cannot just simply roll out a solution ported to c++ on the next release, or even tenth, so perhaps we make it work until we can. however we can set a standard and expectation for new projects to use c++, and we do and set an expectation to target a specific std. i see this sentiment quite a lot on hackernews -- feels like a lot of people saying "git gud" -- i would expect a lot more nuance applied here.
- gnulinux996 1y agoOut of curiosity, what is an example of a regulation that specifies using a specific programming language and/or toolchain out there?
- pjmlp 1y agoWhile using old compilers in certified domains is a common problem, unless we are talking about stuff like PIC, the choice of reaching out to C++ compilers is a given, even if what they might support is between C++98 and C++11.
- brunker2 1y agoWhy would you write C++ if you can get the same result by jumping through a few hoops with C?
- zabzonk 1y agoTemplates in C++ require language support - you can't simply implement them with "a few hoops" in C.
- petabyt 1y agoThe real answer: it's more fun this way.
- pjmlp 1y agoBecause some people really hate C++ to their bones, hence this kind of stuff that keeps coming up. I was really disappointed that Microsoft decided to backtrack on C++'s is the future, after the new found Linux and FOSS love. https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99/ https://herbsutter.com/2012/05/03/reader-qa-what-about-vc-an... https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/ https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-... Not that it matters much, as nowadays C and C++ have a new policy at Microsoft due to goverments and cyberlaws. https://azure.microsoft.com/en-us/blog/microsoft-azure-security-evolution-embrace-secure-multitenancy-confidential-compute-and-rust/ https://azure.microsoft.com/en-us/blog/microsoft-azure-secur... https://blogs.windows.com/windowsexperience/2024/11/19/windows-security-and-resiliency-protecting-your-business/ https://blogs.windows.com/windowsexperience/2024/11/19/windo...