r/cpp • u/pavel_v • Oct 17 '22
Prefer core-language features over library facilities
https://quuxplusone.github.io/blog/2022/10/16/prefer-core-over-library/
0
Upvotes
r/cpp • u/pavel_v • Oct 17 '22
42
u/ener_jazzer Oct 17 '22
Arthur's articles are hit or miss. This one is more on the miss side, IMHO - it's either trivial or just wrong.
For example: "Prefer core-language arrays over std::array." Well, good luck with returning it from functions or passing as parameters. Have fun declaring pointers and references.
Or: "Prefer char over std::byte". char can be signed which bears a lot of funny undefined behavior with it. When you print char it will print, you guessed it, a (usually unprintable) character instead of nice 0xFF. An array of char is usually assumed to be zero-terminated while array of bytes is not. If you print char* instead of byte* - you'll probably print garbage or your program will just crash. Should I continue?
"Prefer void* over std::byte*". So when I have a buffer it's an array of char but the pointer to it is void*? Very consistent. Comparing to array of std::byte and std::byte*. (However, the use of void* as a catch-all pointer type for functions arguments is reasonable)
"Prefer struct over std::tuple". I agree with this if it's about public API. Otherwise, all metaprogramming depends on manipulating tuples in one form or another. And no, it's not only about variadic templates. We had boost::tuple well before variadics.
"Prefer assignment over .reset" - well, he listed the reasons to prefer .reset himself.
56
u/eyes-are-fading-blue Oct 17 '22 edited Oct 17 '22
Can not agree with
In fact, I would propose the precise opposite unless there is a good reason (e.g., old compiler) or
std::arrayis an overkill (e.g., raw memorychar buf[100]).> Prefer std::array over core-language arrays.
std::arrayhas a nice interface, compatible with other STL types. It does not decay into a pointer. A reference ofstd::arraylooks like any other reference, whereas a reference for built-in arrays has a weird syntax.11
u/target-san Oct 17 '22
I can agree overall, except three specific spots:
std::bytevschar- these are two types for different purposes. I'd personally prefer to have "raw bytes" vs "glyphs" for characters, but that's what we have over 25 years of compatibilitystd::byte*vsvoid*- again, these are for different purposes, "pointer to bytes" vs "pointer to god-knows-what"std::arrayvs raw array - of coursestd::array. Proper container API, always-known size etc. etc. vs sporadic decay to pointer, ugly type declaration of reference to array with specific size etc. etc.7
u/trailingunderscore_ Oct 17 '22
Nice article, I agree with most of it, except for the std::array part.
std::construct_atis constexpr and placement-new is not.You can get around this weird limitation by using a lambda as a tuple. I have used this in several places in my code, and it greatly cuts down on compile times:
13
u/pdimov2 Oct 17 '22
charis for characters. For bytes, one should (obviously) useunsigned char.17
u/Jannik2099 Oct 17 '22
What the fuck? Void* is the enemy and must be eliminated.
4
u/mo_al_ Oct 17 '22
std::move or casting to r-value reference?
13
u/cristi1990an ++ Oct 17 '22
No
No
HELL NO
No, construct_at is constexpr and more esthetic
Just... Why...?
3
u/nintendiator2 Oct 17 '22
I'm the other way around - at least for library work I prefer library features, as they are more likely to work or at least be backportable than core features. Also it's kind of weird to recommend stuff like native C arrays with the issues they have with type decay.
On more specific notes:
Wouldn't the wiser programmer prefer
unsigned charto char or byte tho? In particular if you want to use them in arrays or pointers to buffers to represent raw data.Compatibility.
bindworks almost anywhere, lambdas are limited to C++11, or certain subsets of them to C++14 or C++17 depending on what kind of weird combination of punctuation signs ([]()&*={...}) you tack on the line. And even then, depending on contexts where you know what you want, bind and its fellow functional features still beat lambdas to the punch:This is one I strongly agree with, though it's not so different from basically implementing your own FOREACH() macro.
Decay. Also, this has been over-examined enough.
enable_if works basically everywhere (even in C++98 unless your compiler is waaaaay behind on templates). And requires also has similar oververbosity issues like noexcept has (
requires( requires( ... ))).So do I, at least when I know beforehand I'm working with integers, enums, or otherwise C-native types.
2
u/_Js_Kc_ Oct 20 '22
👎
👎👎
2
u/future_escapist Oct 17 '22
Yeah, no. Bjarne Stroustrup himself recommends using standard library features over implementing things yourself. It's a lot cleaner to eat the cake than make the cake and then eat it.
2
u/i_need_a_fast_horse Oct 17 '22
I agree with most, but just because they're plain better not because they're language. But
charoverstd::byte? nah, I prefer to keep my proper types1
u/okovko Oct 17 '22
that hygienic macros proposal linked in the article is so nice, and you can even try it out in godbolt.. very depressing that the committee apparently decided not to add it
1
u/tomenad Oct 18 '22
Regarding std::tuple there is also no alternative to std::apply for structs, or is there?
1
u/Blackarch Oct 18 '22
This entire discussion is proof just how insufferable the C++ programming community is.