73 points · by fanf2 · 6y ago · 99 comments
C++ attempts something messy, which is to take C source and generate objects conforming to this ABI. In doing so, the only sane thing to do is to respect the C spec to transform C source in a binary blob that others can understand.
C++ should stop attempting to parse C. Just use a C compiler, create a binary blob and interface with it from the C++ compiler. Attempts to make both language compatible on some common subset makes them both worse off.
This requires that the Microsoft C compiler is updated to the latest C standard though. Until recently their recommendation was to compile C code with the MSVC C++ compiler.
That seems like an awful amount of work just to prevent C++ programmers from using any existing C library.
You could use a few vectors or other container objects, which is a tradeoff - it gets a little bit more comfortable to spot/debug OOB issues (but valgrind and sorts can also help with the occasional problem). In exchange you are required to structure your data and code in a particular way to use these structures, which is a drawback. Another drawback is slower compile times (significantly so with std:: containers -- don't have a lot of experience with hand rolling C++ abstractions).
I often prefer having globally allocated (dynamic) arrays, which are unproblematic with respect to OOB or memory/allocation safety. I use a simple macro to wrap allocation of arrays to remove the boilerplate. It becomes like "realloc_array(pptr, FooType, 42)" or even "realloc_array(pptr, 42)". Local allocations are more often not easily dealt with using Memory Arenas, which is superior to alloc/free in constructors/destructor.
I even prefer using plain pointers with separate size variables for dynamic arrays. That's because "size :: array" isn't a 1::1 relationship. More than 50% of the time, I have more than one array that applies to the same size variable. Sometimes, there are multiple index/size pairs that apply to a single (or multiple) arrays.
I prefer staying with C because it keeps me focused. Sure, sometimes I wish for the occasional fancy feature, but then taking the time to think about a clean approach that solves the problem with limited features often benefits code quality and readability.
the number of bugs that the compiler has saved me by e.g. having a simple Id<T> type tag instead of ints as used by every C library under the sun is absurd.
How do you go about Id<T>? The way I see that something like this can work out and give small advantages is that T would be an abstract type (a phantom or tag type, or whatever C++ guys are calling it). And all arrays that are meant to be indexed using Id<T> (while the array element types could be whatever) would depend on this type and have to be implemented using a special wrapped array type with overloaded operator[].
I don't know, this might come with many disadvantages.
All the stuff goes out the roof when you're now dealing with start indices + slices, i.e. doing "pointer" arithmetics.
My general feeling is that this Id<T> approach might show problems similar to the idea of using typed units, like wanting to prevent the expression "1W + 3s" but then making it terribly cumbersome to do real work, e.g. "1W * 3s" which would be perfectly valid.
In one C project I resorted to using about 20-30 typedefs like:
typedef int Expr;
typedef int Symbol;
etc. And I used these typedef names in function signatures. Variables also were named like expr, parentExpr, symbol, etc. It worked wonderfully. I had maybe 1 mistake where I submitted the wrong "type" in 6 months. And I'm someone who makes a lot of stupid mistakes.How ironic that commercial vendors get crucified for C and C++ language extensions.
Sometimes I've found that it's just easier to stick with plain C that way you don't ever feel tempted.
[1] https://github.com/floooh/bookmarks
[2] https://github.com/nothings/single_file_libs (here, C/C++ usually means "written in the common subset of C and C++")
A lot of very successful and robust software is written in C. The preference for the C language is certainly not due to a personal opinion of only one person.
C is one of the most popular languages for high-performance code. Most famous example would be the Linux kernel but literally anything that needs the best optimizations possible eventually comes down to C.
Good tendency however, now both C and C++ developers started experimenting with Rust, probably creating unified community and platform.
You have to use a subset of c++. Nobody agrees what that subset is. You have to be super-vigilant in code review. Features invite their use.
It's the lack of features in c that make it attractive. No magic. You want an object or a virtual, code it up if you really mean it is not just a keyword. You take responsibility for all the code running.
Every time I've cut a tonne of latency from a trading engine it's that. People relying on library and compiler without taking responsibility for it. STL is great. Except if you're actually performance critical, when you can beat it easily by solving your problem, not someone else's solution for everyone's possible similar problem.
That said, I tried to reproduce something similar, and it seems the issue only occurs in my example due to external linkage (adding 'static' fixes it)... but I can't claim this will always resolve the issue: https://gcc.godbolt.org/z/1vbqo3
> I had been harboring some suspicions that our big codebases might benefit from the application of some more of the various �modern� C++ design patterns, despite seeing other large game codebases suffer under them. I have since recanted that suspicion.
I think this shows that C++ works better in controlled (i.e. corporate environments), whereas C is often preferred in Open Source.
For example, GCC's C++ front end will produce a different parse tree for the same C code as the C front end, resulting in a slightly different collection of basic blocks and data flow analysis (obviously, for very trivial examples, it will be identical). The result after all the gimplification and and different middle-end passes and rtl transformations you can end up with a surprisingly different set of generated instructions for the same code fed to gcc and g++. Equivalent, but different.
That's not really true. A single compiler may support many languages but that does not mean code written in those languages ends up as the same binary.
I'm yet to see a 'modern web browser' that is fully written and only written in C. (C bindings don't count)
Another big one is implicit casting of void ptrs. I cast void pointers to specific types A LOT. There are many situations where you have to cast void pointers (that come from an abstract API that only deals with untyped memory) to specific types (higher-level application code), and I can't really be sure, but I think there isn't a nice way to do that in C++.
It might be because C++ wants you to use their elaborate abstractions and templates, which in many situations is just fluff that makes programs harder to read and maintain.
that's a pretty harsh take on it - how would you design it considering that initialization order has to be respected (else a lot of use cases would break; it's definitely not uncommon to have a member of a struct to depend on a previous member being initialized) ?
I have yet to find a particularly blocking use of the feature when test-driving it - it's on the other hand a great boon for readability (and definitely puts a nail in the coffin of named function parameters proposals imho).
Not sure if this is impossible in C++, I think some of the alignment functionality added in the past several versions might cover this, but it is not as simple.
IMHO, Not a big reason since every compiler offers some non-standard keywords to cover this use case, but a reason nonetheless
I think the ship has sailed on proper compatibly, and thats ok with me, let them be different. More compatibility is not worth braking any existing code.
What do you make of Zig?
As a C programmer I dont want clever features. I want the simplest possible where nothing happens without me explicitly typing it. I'm much more interested in better debuggers, and tool chains [1]. Every time you create a new language or a significant new version of a language, you have to start over with the tool chain.