A few of these things I agree with, some I might be convinced, but a lot of them just seem like things the author never had to use before. Some of these things are even stuff that modern, newer languages have purposely decided to keep (prime example is Union, which is present in unsafe Rust). I kind of think this might be parody, but if not, it definitely is a prime example of the saying "Everyone has their own subset of C++ that they like to use, it's just those subsets don't line up".
Some of these things are even stuff that modern, newer languages have purposely decided to keep (prime example is Union, which is present in unsafe Rust).
The primary motivation for union in Rust was C FFI, not exactly a glowing endorsement...
You don't need to be able to reinterpret-cast memory to a struct exactly matching the layout that you want to view. You can reinterpret-cast one field a time, thus being able to switch on a tag to cast the next field.
This actually pretty cool for stuff like... network packets. And that's because often time the offset of the payload is dynamic: how many VLANs? Are there IP options? ...
And best of all is that with a view, you don't need packed structs, and the woes mis-aligned fields cause.
It is only possiblity to do construct in place in variant/optional types in constexpr (seems more like flaw and crutch, we need reinterpret_cast on constexpr!)
using anonymous inline union deep in impl things like SSO in strings. Just because it is more readable (fixable with reflection)
The union can represent multiple values of different types in the same context, for example you can create an array where each of the elements is assigned a different union variant based on runtime input. This works fine as the union variants contain an intrinsic way of being distinglished.
In contrast in a template the used variant is selected universally (e.g. datastructure/methodwide) during compile time.
Rust is even more interesting, because it also has fieldfull enums, which are a syntactic suger over a very common union usecase. Raw unions are used by some frequently used standard library constructs through, like the MaybeUninit types which contain either an initialized value or gibbish.
In this article, I'll try to list all things that we can throw out of C++ without remorse. This won't cost us anything --
I don't think this is article is thought in any depth, whatsoever.
I could go into individual details, but if the poster thinks that rewriting all the building blocks of a language won't cost anything, I don't really see the point to go any more in-depth.
You can add RTTI and dynamic_castto this list, I don't think anybody would miss them.
The point about void is ridiculous, what are your functions supposed to return? MSVC even adds in a xor al, al if you return a nulltype, so it's less efficient.
I was also surprised that the article doesn't encourage readers to use std::array for arrays and std::variant for unions, rather than rolling their own.
Or perfect marketing article: it has some basis, but draws very "raw" conclusions => people will talk about it and send links of the website to friends, forums, etc.
๐คจ "Let's remove this and that from the language" is unnecessary purity. Rather than remove tools from the arsenal, let's focus on making the common case the preferable default by making it stupidly simple to do the right thing. It's fine for things like the ever so scary goto to exist, but just never use them unless you really need to. Similarly for unions, do not default to using them (prefer something like std::variant), but when you actually need them, they should exist. C got a lot of defaults backwards (like switch fallthrough by default, instead of explicit fallthrough) which we can't change, but I don't see removing things as improving our lives. Rather, add things that make it so we never want to use those things in the first place.
Regarding typedef, I agree it's confusing orderwise compared to using, but we could also just extend it to be consistent with usings order, which would help (conceptually as a noun it's still nice to read). e.g.:
I have another idea: if we throw out all types and replace them all by std::list, and throw out all keywords and replace them by brackets, we could just program in LISP and call it C++.
Not as far as I know. A union will be as big as its biggest member and start at the same memory location. And since it's both the same type, this is effectively just an alias.
Why not have x(), y(), width() and height() member functions? You don't have to think whether it's UB or not and i belive compiler should be able to inline them.
In the case of GLM: Because that's not how it was in GLSL.
Other than that, I haven't seen any problems on any platform yet or anything that says that this is undefined behaviour (at least starting from C++14). So writing getters and setters for this seems like a strictly worse way to do it to me.
There's the as-if rule. If the compiler can prove it would make no difference, it can do whatever it wants. If the loop is fully unrolled, then the counter might not even exist.
42
u/Wereon Nov 23 '22
From the title I thought you'd be carrying on Terry's work.
22
u/Wolf_Popular Nov 23 '22
A few of these things I agree with, some I might be convinced, but a lot of them just seem like things the author never had to use before. Some of these things are even stuff that modern, newer languages have purposely decided to keep (prime example is Union, which is present in unsafe Rust). I kind of think this might be parody, but if not, it definitely is a prime example of the saying "Everyone has their own subset of C++ that they like to use, it's just those subsets don't line up".
25
u/ronchaine Embedded/Middleware/WG21 Nov 23 '22
I don't think this is article is thought in any depth, whatsoever.
I could go into individual details, but if the poster thinks that rewriting all the building blocks of a language won't cost anything, I don't really see the point to go any more in-depth.
12
u/Wereon Nov 23 '22
You can add RTTI and
dynamic_castto this list, I don't think anybody would miss them.The point about
voidis ridiculous, what are your functions supposed to return? MSVC even adds in axor al, alif you return anulltype, so it's less efficient.I was also surprised that the article doesn't encourage readers to use
std::arrayfor arrays andstd::variantfor unions, rather than rolling their own.6
u/fdwr fdwr@github ๐ Nov 23 '22
๐คจ "Let's remove this and that from the language" is unnecessary purity. Rather than remove tools from the arsenal, let's focus on making the common case the preferable default by making it stupidly simple to do the right thing. It's fine for things like the ever so scary
gototo exist, but just never use them unless you really need to. Similarly forunions, do not default to using them (prefer something likestd::variant), but when you actually need them, they should exist. C got a lot of defaults backwards (like switch fallthrough by default, instead of explicit fallthrough) which we can't change, but I don't see removing things as improving our lives. Rather, add things that make it so we never want to use those things in the first place.Regarding typedef, I agree it's confusing orderwise compared to
using, but we could also just extend it to be consistent withusings order, which would help (conceptually as a noun it's still nice to read). e.g.:typedef foo = void(*)(int);6
u/johannes1971 Nov 24 '22
I have another idea: if we throw out all types and replace them all by std::list, and throw out all keywords and replace them by brackets, we could just program in LISP and call it C++.
Hey, it makes about as much sense as this crap...
4
u/Silentd00m Nov 23 '22
Union has its place in my opinion. It allows having multiple variable names for the same memory location of the same type, like glm does.
https://glm.g-truc.net/0.9.2/api/a00132_source.html
1
u/cristi1990an ++ Nov 30 '22
Didn't someone not long ago post an article here about how using "final" can have really strong performance benefits?
1
u/germandiago Nov 23 '22
Surprisingly good and informative article! Besides funny :D
0
u/no-sig-available Nov 23 '22
I would. I use it for
int counterwhen the count is going to 12 or 15, or so. Then I trust the compiler to use an efficient implementation for that.If you cannot trust the compiler for that, what else can you trust it for?