11 ms·
Three new utility functions in C++23
- kazinator 4y agoNo thanks, I'm going to keep calling abort (ANSI C, 1989) to indicate "control stops here": Code after an abort() call is unreachable. (Or after any function attributed noreturn). GCC and Clang know this, and do things accordingly. For instance, I've seen GCC emit code which assumes that ptr is not null after ASSERT(ptr != NULL), because the custom ASSERT macro called an __attribute__((noreturn)) function in the null case. abort() has defined behavior; it terminates the program abnormally, as if by raising the SIGABRT signal. Your own function attributed __noreturn__ can have whatever behavior you want it to have. The one in the ASSERT macro I alluded to above calculates and prints a backtrace and other useful information.
- secondcoming 4y agoThey missed a trick with `std::to_underlying`. It should work on all types, not just enums. By not doing that you still have to do `std::is_enum_v<T>` somewhere. For types that aren't enums it should do nothing (maybe `std::identity`)
- fluoridation 4y agoSeems easy enough to add your own overloaded function that does that. But then again, std::to_underlying() doesn't do much to begin with; it just casts to std::underlying_type<T>::type.
- hoseja 4y ago>Network protocols specify big endian for the order of transmission Only in the parts specified by the protocol (headers etc). I encourage everyone sending data over network in a novel way to just use little-endian.
- halayli 4y agoThat's not a good advice. Only if the sender and receiver are guaranteed to be running on little endian architecture you can make such a claim. A better advice is to always consider the endian-ness when designing protocols and have a strategy to handle it.
- dontlaugh 4y agoI think they mean for any new protocol, defining it as little-endian.
- unnah 4y agoYou could call it the krowten byte order.
- patrec 4y agoHow is that "better advice"? Big endian architectures are pretty much dead (x86, ARM and RiscV are all little endian; some ARM chips are bi-endian, but not Apple's) and there's no discernible compelling advantage that would allow a comeback. You absolutely want to specify the byte order in new protocols as little endian.
- a1369209993 4y ago> and there's no discernible compelling advantage Actually, for wire encodings, there is, although I've (I-think-)literally never seen any proponent of big endian bring it up (versus the bullshit "it's human-readable" nonsense[0][1]): big endian encodings of unsigned numbers have lexicographic order that matches their numeric order. The most obvious concrete example of why this is useful is a keys-sorted encoding of a hash table: if you encode keys in size-type-value format, you can check sortedness by lexicographic order of type-value strings (which means you can add new types without old software needing to know how to compare them), and you'll get integer keys in inspection-friendly numeric order rather than semi-random order. (Encoding negative numbers with a type id of T_UINT-1 lets you extend this to them as well.) At a more abstract level, where (zero-padded) little-endian numbers have the same value at different granularities, this means that big-endian numbers have invariant lexicographic order at different granularities: two strings viewed as bits, bytes, or uint32s are consistently in the same order. You can kind of use reverse-lexicographic order for some of this, but there are obvious problems with sending data in value-type order rather than type-value, so forward-lexicographic tends to be strongly enforced. 0: "You mean for arabic numerals, except not actual arabic numerals, because Arabic is written right-to-left, so the numbers are little-endian there, but ended up big endian because they stayed least-signifiant-digit-right rather than least-signifiant-digit-first when imported into Latin." 1: "Also, so (supposedly) is decimal and sign-magnitude, but we've (agonizingly slowly) learned that those aren't good ideas."
- stinos 4y agoI love how over the past decade my own C++ utility library has been continuously shrinking because with each update there are more and more utility functions (like the to_underlying this article mentions) and even complete libraries (like <filesystem>) which replace self-written or 3rd party code.
- gpderetta 4y agoI always worry as much as anyone else on each new release for the additional complexity ("the committee is out of control!!!!1!!eleven!"), but on each compiler upgrade when I actually get to use the new versions of the standard I'm always pleasantly surprised about all the little low-key quality of life improvements.
- einpoklum 4y agoWhile that's true, when you get to a point where you have to write a small library - so that you need to cater to all of the language and standard additions - that's when you start experiencing pain. How do I expose the right iterators and sentinels? What do I have to specialize? Do I need to define concepts? Do I need to use concepts from elsewhere? I am often at a loss...
- legends2k 4y agoWhile this is true to some extent, trying to be overly generic and solving for all cases is the root of the analysis-parslysis. I end up solving just the part that's needed for the problem in hand but accepting meaningful compromises and trade offs when faced with this issue.
- gpderetta 4y agoPremature generalisation is the root of all evil. According to Stepanov generalisation is something you are supposed to discover as you develop a program, not something you do from first principles.
- yrro 4y agoHmm. How often do people actaully want to std::byteswap as opposed to "convert this value from native byte order to big-endian" or "convert this value from little-endian to native byte order"? i.e., the functions documented in https://man7.org/linux/man-pages/man3/endian.3.html https://man7.org/linux/man-pages/man3/endian.3.html (why oh why are they not also documented in the GNU C Library Manual...)
- Karellen 4y agoYeah, for portable code you also need a function to tell you if you're on an architecture where you need to do a byteswap for the data you have. e.g. you know you have data in little-endian format - do you need to swap it to work with it natively? That depends. Maybe having something like convert_be() and convert_le(), one of which is a no-op and the other does the byteswap (depending on your arch) would be better. It removes the duplication of e.g. htobe() and betoh() which are exactly the same function, while allowing the caller to not worry about which architecture their code is compiled on.
- apaprocki 4y agoI view it more as a platform-independent building block to be used by library authors. For instance, here's our suite of byte order macros that this will not simply replace: https://bloomberg.github.io/bde-resources/doxygen/bde_api_prod/group__bsls__byteorder.html https://bloomberg.github.io/bde-resources/doxygen/bde_api_pr...
- gpderetta 4y agoYes, there is also std::endian::{big, small, native} that can be used to check if you need to byteswap or not. So a generic hton* is just: auto to_network_endian(auto std::integral value) { if constexpr (std::endian::native != std::endian::big) { return std::byteswap(value); } else { return value; } } ntoh is left as an exercise :). edit: in practice I think std::endian and std::byteswap is a compromise between those that wanted a simple {to,from}_network_endian and those that wanted strongly typed wrappers to prevent mixing object with distinct endianess (a-la boost::endian). As the commitee couldn't reach consensus, this is the compromise and you can build your own thing with these portable bits.
- CJefferson 4y agoI understand why it's there, but I do find it fun that when many people are trying to reduce undefined behaviour in their code, std::unreachable is literally defined as "this is undefined behaviour, use that to optimise". I suspect 99.9% of uses of std::unreachable would be better replaced by abort. (There will be those times when the code is correct and the optimisation gains are worth it -- but they will be rare).
- actionfromafar 4y ago+100 I felt physically ill when I read: It’s intended to be used when you know you have an execution path in your code that cannot be reached but the compiler cannot figure that out. It felt like saying to the compiler, "please, find a way to make my program break even more easily". Exactly not what I need.
- gpderetta 4y agoYou might not need it, but gcc/clang have __builtin_unreachable and msvc has __assume and both are used extensively for optimizations. std::unreachable is just standardizing existing diverging practice.
- fluoridation 4y agoIf a code path that's supposed to be unreachable is reached then the program is already broken. Unless it has a bug, a compiler will not make a program more broken. At worst (or best, depending on how you look at it) it will only make any bugs it already has more obvious.
- actionfromafar 4y agoYes, but what was unreachable may get reachable as I do changes to the program. I see the utility when doing highly optimized library code. But for my purposes I much prefer to stick something which flags me (exception or logging or whatever) of "this should never happen" instead of crashing. (Undefined.)
- XCSme 4y agoWhy is it called "byteswap" and not "bytereverse"? "Swapping" can be done in any number of ways, but only one way to reverse.
- deleted 4y ago[deleted]
- siknad 4y agoReversing bytes sounds like reversing their bit orders to me.
- fluoridation 4y agoCould be called "byte-wise reverse".
- XCSme 4y agoThat would be "bitreverse" I guess, plus their description of byteswap[0] is exactly that: "Reverses the bytes in the given integer value n." I would expect the description for "byteswap" to be something like "swaps the given bytes of an integer value n." and be called like byteswap(x, a, b), where a and b are the indicies of the bytes to be swapped. [0]: https://en.cppreference.com/w/cpp/numeric/byteswap https://en.cppreference.com/w/cpp/numeric/byteswap
- secondcoming 4y agoThere's an x64 instruction called BSWAP.
- pm215 4y agoI don't know, but 'swap' seems to have become the standard term for the operation, used in places like the C bswap family of functions and the x86 'bswap' instruction. My (totally unsupported and unresearched) guess is that it became popular as a term when 16-bit architectures were common -- "swap the bytes in a 16 bit value" is unambiguous. The Arm architecture does call this operation "reverse bytes", though, so it's not universal to call it "swap".
- a2800276 4y agoMy first impression is that all three of these are clutter to an already very cluttered language ... 1.) Coming from embedded programming, I can see the utility of `std::unreachable`. But shouldn't this be a compiler directive? Or a standardized #pragma? Can someone more knowledgeable in C++ say whether using functions as markers is a common mechanism in std:: ? 2.)Maybe the example is bad here, as it doesn't even save typing. (18 chars for `std::to_underlying` vs. 16 for `static_cast<int>`. The 'old' variant seems more expressive to boot. If anything, how about `static_cast<auto>`? or <underlying> ... 3.) The usefulness of `std::byteswap` to convert data to network byte order seems trivial vs. the venerable old `htonl` family of functions. `std::byteswap` seems more like intrinsics meant to expose possibly present target machine instructions to the user. Like `std::unreachable` this is probably of most use to embedded / low-level programming. This may be a deficiency in the article... If it's not obvious, I'm not a big C++ fan and read the article with C-tinted glasses :)
- kazinator 4y ago> But shouldn't this be a compiler directive? Lisp perspective: anything that can be a function almost certainly should be a function (and not a special operator or macro). std::unreachable() does not have any arguments; therefore it doesn't need any special argument evaluation semantics that would require a compiler built-in. The way C and C++ work, compiler directives are keywords and not identifiers. Keywords are not namespaced. Introducing a keyword called "unreachable" is problematic; far more so than a new element in the std namespace. My only problem with std::unreachable is that I would never use it over std::abort. Nobody needs a function whose only job is to invoke undefined behavior (from which it is then assumed that it is not reached). It's a good cold day, so I can almost hear the Rust people laughing in the distance.
- steveklabnik 4y agoTo be clear, Rust has this exact same functionality, with an intrinsic as well as a regular function that wraps said intrinsic. The only difference is that it’s marked unsafe. https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked.html https://doc.rust-lang.org/std/hint/fn.unreachable_unchecked.... There is a safe version that is just a wrapper around panic! with a standardized message.
- nynx 4y agoWhy on earth are these in C++23 and not C++11? There are a ton of things that should’ve been standardized over a decade ago but only show up in C++20 or later. `std::span<T>` is a huge one. The mind boggles.
- Cthulhu_ 4y agoBackwards compatibility and a slow moving industry; adding more to a standardization process will only postpone the release. More and more programming languages are moving to a more lightweight or scheduled release schema though, e.g. Java that had been stuck in limbo for nearly a decade due to design-by-committee and backwards compatibility concerns by major players.
- bluGill 4y agoTrying to get everything is why C++11 wasn't c++07. The committee spend several years trying to polish things instead of doing a new release. (the committee thought they weren't allowed to release anything before 06/07, otherwise some of what was in C++11 could have been in C++01 - or maybe c++03 but with more in it). Eventually you need to say stop right here, what is done is what we will release, what isn't done will have to wait.
- gpderetta 4y agoIt took 10 years to get c++11 out as it was...
- oreliazz 4y agoTo get something into C++ someone has to actually write a paper and propose it to the standards committee who has to vote and agree on it. You can't just have some dude write a span.hpp and go "yep, that's going into my compiler v2" - not how standards work.
- nynx 4y agoI would’ve much rather have gotten span into C++11 than variadic templates, for instance. Definitely seems to me like a paper about span would’ve been a lot shorter and easier to write than one about variadic templates.
- donatj 4y agoSilly question from someone who hasn’t written C++ in 20 years and only very vaguely remembers it - if a code path is unreachable, why have the path at all? Is a default on a switch required? Is lack thereof a compiler warning or something? It’s been a very long time; I only ever seem to recall that kind of “all paths must be handled” from functional languages.
- f1shy 4y agoI think the examples are not the best...
- Koshkin 4y ago‘default’ is there by default anyway. This new feature allows to explicitly state that there isn’t one.
- beached_whale 4y agoIt can also improve codegen and is a building block of building an assume like macro to convey preconditions to the compiler and optimize potentially on that. The other is at the end of a function that can be guaranteed to never reach that point so that the compiler can know. Its a low level tool and shouldn’t be used without caution
- gpderetta 4y agoIt can help both optimizations and static analysis. For example in the switch case in principle the compiler can avoid doing bound checks on the switch jump table. For static analysis it can help flags paths that can actually happen as erroneous.
- Cthulhu_ 4y agoNot C++ specific, but the example given - a default case that is not expected to ever be hit - is a "cover all bases" thing, to avoid undefined behaviour. Without the default case, what should happen? In the example given, it would just do nothing, but it would do so silently, which could lead to hours of developer time wasted trying to figure out why it doesn't do anything. But I can imagine other use cases that are like "this is not supposed to happen" that can cause major issues like buffer overflows. Better to be more defensive and write code that basically says you are aware of code that shouldn't be reachable.
- deleted 4y ago[deleted]
- agluszak 4y ago> A typical use case for this function are switch statements on a variable that can take only a limited set of values from its domain. For instance, an integer that can only be between 0 – 9. Here is a simple example with a switch that checks a char value and executes operations. Only a limited number of commands are supported but the argument is checked before invoking the function so it shouldn’t be possible to receive other values than already handled in the switch. This is a bad practice. Parse, don't validate - https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/ https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...
- fooker 4y agoOkay, what about when you are writing the parser?
- simias 4y ago>Byte swapping is important when transferring data between system that use different order for the sequence of bytes stores in memory. That seems like a glaring footgun to me, to the point where I think I must be missing something. What I want when dealing with endianess are "from_little_endian/to_little_endian", "from_big_endian/to_big_endian" function pairs that expand to either nop or a byte swap depending on the host architecture. Exposing the byte swapping directly without this layer on top is asking for trouble because every user will have to make sure that they correctly detect the local endianess before attempting a swap. That's the potentially tricky part, not swapping the bytes.
- gpderetta 4y agoThe std::endian enum makes it very easy to find the native endianness.
- a1369209993 4y agoThat's irrelevant, because there's nothing useful you can do with the native endianness (other than implement (load/store)_(big/little)_N for various combinations of options).
- gpderetta 4y agoyou can use it to implement native_to_be or native_to_le, it seems pretty useful to me. In fact, what can't it do?
- SeanLuke 4y agoI think -- I hope? -- we're seeing the death spiral of this language. C++'s Biggest Problem: unbelievable, extraordinary, just mind-blowing levels of complexity. Solution: add more stuff!
- SubjectToChange 4y ago> I think -- I hope? -- we're seeing the death spiral of this language. In a lot of metrics C++ has never been more popular. And given the direction of heterogeneous programming, C++ is here to stay. > Solution: add more stuff! In a lot of ways, yes, that is the solution.
- ratorx 4y agoI don’t necessarily disagree with the underlying point, but this it’s a weird change to comment on. The complexity explosion in C++ is due to extra language features etc. not 3 extra, probably well documented, quality of life functions in the standard library. std::unreachable is the most atypical “function” in this post, but from a code readability perspective, it is pretty clear what it does and it doesn’t really add any new concepts to the language (UB has always been a footgun). Also, if you want to preserve backward compatibility, all you CAN do is add more abstractions and hope they are simpler to understand and cover the majority of the use cases that the old ones did.
- pjmlp 4y agoWhile C++ is indeed a complex beast, mastering the whole of Java, C#, F#, Scala, Python, OCaml, Haskell in their latest versions, standard libraries and changes between major releases isn't any easier.
- Aardwolf 4y agoWhy is std::unreachable a function, and why does it require including a header? It sounds like the functionality of unreachable is to inform the compiler of something, which is what a core language keyword, like "if" or "for", does. Of course then there could be name collisions with the new keyword, so being in std:: might be a solution for that. But that is inconsistent, sometimes they solve this with prepending/appending __ or _t instead, or using obscure enough names like "constexpr" or "nullptr" that probably don't clash
- gpderetta 4y agoWhy shouldn't it be a function? The global namespace is a scarce resource. constexpr can't be a function because it is not used a such. Nullptr could have been std::nullptr, but it is used often enough that it made sense to put it in the global namespace (but note that it had to be nullptr instead of null to avoid collisions, so only two chars saved). The type is still std::nullptr_t. The C++ standard doesn't really use __ as a prefix, it is a namespace reserved for the implementor. The _t suffix in the global namespace is from POSIX originally then adopted by C; C++ still puts _t names under std. std::unreachable is a very obscure functionality, polluting the global namespace for it wouldn't have been a good idea.
- ryandrake 4y agoKind of off topic, but is there a good "catch up" guide for people who stopped paying attention after C++11? Like a quick summary of just the useful, practical things you'd actually want to use in production, rather than the parts that are only interesting to computer science academics? Most of the "what's new in C++X" guides seem to just dump everything on you at once. I feel like I should get back into C++ but I don't think I really need to care about folding expressions or std::bit_cast.
- eco 4y agoI found Stroustrop's book A Tour of C++ very useful for catching up with C++14/17. It's relatively short and it's easy to skim through parts you already know well. It was just updated for C++20 (with some coverage of C++23). There's always a bunch of CppCon talks for this too. Just pop on over to their YouTube channel.
- deleted 4y ago[deleted]
- jahnu 4y agoHerb Sutter’s Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14
- WalterBright 4y agoIn D an unreachable branch can be indicated with: assert(0); which is used frequently in D. This is a bonus from assert() being a builtin to D rather than a macro.
- a1369209993 4y agoThe problem with the C++23 std::unreachable is that it invokes undefined behaviour. Calling abort (or panic, or whatever D's assert boils down to when the condition fails), would be a prefectly reasonable way to define unreachable. (That is, for example, basically how I define it in my own code:) #define unreachable die("unreachable code reached")
- MaxBarraclough 4y agoAs the article states, they're doing different things. It's not a runtime check. The point of making it undefined behaviour is that it enables the programmer to provide information to the compiler to enable better optimisation. The compiler is permitted to assume the programmer is correct about the path being unreachable, and may infer accordingly.