Earlier quoted context omitted.
That'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.
There are practically no big endian architectures anymore. Little endian is a sensible default. The weird architectures should bare the burden of complexity.
Three new utility functions in C++23
131–140 of 196 posts
Re: Three new utility functions in C++23
#132Earlier quoted context omitted.
1. It is a compiler defined function (`__builtin_unreachable()`), but the issue is that MSVC doesn't have it, so you need a different implementation per compiler [0]. Plus, if a new compiler shows up (besides MSVC/GCC/LLVM), you'd need to investigate what the correct way to express `__builtin_unreachable` is. From a compiler perspective, using a function makes the most sense, since that fits into the existing control…
> Pragmas are processed by the pre-processor, so they aren't appropriate for expressing control flow hints. I don't thing this is remotely true. C++ pragmas were designed with the express purpose of providing additional information to compilers.
Re: Three new utility functions in C++23
#133Earlier quoted context omitted.
> Pragmas are processed by the pre-processor, so they aren't appropriate for expressing control flow hints. I don't thing this is remotely true. C++ pragmas were designed with the express purpose of providing additional information to compilers.
Attributes are better suited for that. #pragma has always just been a grandfathered in hack.
Re: Three new utility functions in C++23
#134Earlier quoted context omitted.
While 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...
While 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.
According to Stepanov generalisation is something you are supposed to discover as you develop a program, not something you do from first principles.
Re: Three new utility functions in C++23
#135My 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 sa…
> ...`std::unreachable`. But shouldn't this be a compiler directive? Probably "committee pragmatism", a stdlib change might be easier to get approved than a language change, and compilers already have builtins for this, they're just not compatible (but the differences can be wrapped in a macro, and since C++ doesn't like to expose macros, it's probably still a macro, but hidden inside a stdlib template). FWIW it look…
Re: Three new utility functions in C++23
#136 assert(0);
which is used frequently in D. This is a bonus from assert() being a builtin to D rather than a macro.Re: Three new utility functions in C++23
#137Re: Three new utility functions in C++23
#138Earlier quoted context omitted.
> At worst (or best, depending on how you look at it) it will only make any bugs it already has more obvious. An abort would make them obvious, surely unreachable makes them less obvious?
The point is that you're telling the compiler "I don't care what happens if control reaches here. Assume it never will and use that information to better optimize the rest". It's not an alternative to abort() or throwing because the compiler still needs to generate code for them.
So this is a “hide bugs but maybe improve performance” function.
Re: Three new utility functions in C++23
#139Silly 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.
int process(my_enum x, state_t state) {
if (x == my_enum::COMPLICATED) return do_stuff(state);
switch (x) {
case my_enum::SIMPLE0: return 0;
case my_enum::SIMPLE1: return 1;
case my_enum::SIMPLE2: return 2;
}
}
and your compiler complains that my_enum::COMPLICATED isn't handled, even though it clearly was. You generally like these warnings, because they do keep you safe, but in this case, you're smarter than the compiler, but it forces you to put something there. With std::unreachable, it will squelch the warning and not emit extra code. For example, you might be tempted to throw an error -- but if nothing else in the function throws, you end up emitting a bunch of unreachable error-handling code that the compiler can't remove.Re: Three new utility functions in C++23
#140Earlier quoted context omitted.
> ...`std::unreachable`. But shouldn't this be a compiler directive? Probably "committee pragmatism", a stdlib change might be easier to get approved than a language change, and compilers already have builtins for this, they're just not compatible (but the differences can be wrapped in a macro, and since C++ doesn't like to expose macros, it's probably still a macro, but hidden inside a stdlib template). FWIW it look…
std::unreachable() feels like it's what std::assert(false) should be, except that std::assert(false) is bizarrely defined (as a no-op) when NDEBUG is defined. This is one of those strange cases where the spec defines behavior that I'd strongly expect to be undefined.