In my experience, unless you’re strictly talking about header files (which I’m assuming you’re not), C code compiling via C++ compilers is usually hit or miss and highly dependent on the author of the C code having put in some effort into making sure the code actually compiles properly via a C++ compiler.
In the overwhelming majority of cases, what lots of folks do is just slap that `extern "C"` in between `#ifdef __cplusplus` and call it a day—that may work for most forward declarations in header files, but that’s absolutely not enough for most C source files.
By the way, a great example of C code that does this exceptionally well is Daan Leijen’s awesome mimalloc [0]—you compile via a C or C++ compiler and it simply just works. It’s been a while since I read the code, but when I did, I could immediately tell the code was written with that in mind.
um, not unless explicitly written to so.
IMO there's no point even attempting a blind recompile of C as C++. A transpiler tool would be needed.
If you did that and implemented marking passing size info over function calls then you could probably mechanically convert crappy foo(void *buf, int sz) code to foo(slice_t buf) which would be a lot safer to maintain.
You're asking about old C code - that code won't compile as C++. And newer C code would likely also be cleaner and not benefit much from being compiled as C++.
(And, C++ is a net negative over C anyway due to much poorer reviewability.)
I mean, most of C just compiles as C++ and does the right thing. There may be some caveats with UB (but less if you don't switch compiler vendor) and minor syntactic differences, but I guess most of them could be statically checked.
Or is there any other (performance/size) reason to not do this?