yobbo 2 minutes ago

It might be more useful with a signature like maybe_divide -> maybe(int) -> maybe(int) -> maybe(int) ... and then a set of operations over maybe, and functions/macros for and_then(), or_else(), etc. It would be interesting to see how ergonomic it could get.

nly 4 hours ago

GCC generates essentially the same assembly for C++'s std::optional<int> with the exception that the result can't be returned in a register (a problem which will go away if the function is inlined)

https://gcc.godbolt.org/z/vfzK9Toz4

  • account42 4 minutes ago

    > with the exception that the result can't be returned in a register (a problem which will go away if the function is inlined)

    It's still a damned shame. Same for not being able to pass optional/unique_ptr/etc in a register.

    We really need a trivially_relocatable attribute.

  • uecker 3 hours ago

    Yeah it a amazing a far C++ has come, soon it will be almost as good as C.

    • pjmlp an hour ago

      Except that it is much more safer to use the type system than pre-processor glue based on text replacements, with more interesting error messages than templates.

  • wahern 2 hours ago

    Is this because std::optional isn't trivially constructible?

    • pwdisswordfishz 2 hours ago

      Destructible. Opportunistically making it trivially destructible does the trick.

nextaccountic 2 hours ago

> Here, instead of handling the error condition, I create an lvalue that points nowhere in case of an error because it then corresponds to (({ (void)0; })), relying on the null sanitizer to transform it into a run-time trap for safety.

Isn't this undefined behavior?

  • lmm 2 hours ago

    In standard C yes. But any decent C compiler will offer stronger guarantees than the minimum that the standard requires, and presumably the "null sanitizer" they're referring to is one of them.

    • pjmlp an hour ago

      As usual, the problem is not what they have been offering for the last decades in tooling, rather what developers actually make use of.

      Unfortunely many keep needing education on such matters.

rowanG077 2 hours ago

At this point I always wonder why people who write stuff like this don't just move to a different language. You are introducing insane amounts of hidden complexity(see also the other posts on that blog). For something that just exists in other languages. Or maybe this is just a fun puzzle for the author, in which case it's totally fine.

  • amiga386 14 minutes ago

    Quite. There's standard POSIX behaviour for this. Divide by zero and execution continues, safely, in your SIGFPE exception handler.

  • throw-qqqqq 2 hours ago

    You don’t always get to choose your language. Especially in the embedded/firmware area of software development, C is the most widely available option, if not the only option besides ASM shrugs

    • pjmlp 20 minutes ago

      Unless you are talking about PIC and similar CPUs, there is hardly a modern 16 bit CPU that doesn't have a C++ compiler available as well, assuming that we still consider 16 bit modern for whatever reason.

      Heck I learned to program in C++, back when DR-DOS 5 was the latest version, and all I had available was 640 KB to play around, leaving aside MEMMAX.

      Nowadays the only reason many embedded developers keep using C is religous.

    • fuhsnn an hour ago

      The said library is a bit farther away from the C that is widely available. It relies on C23 features, GNU statement expression, GNU nested function, sanitizer runtimes, VLA types and a very niche pattern of sizeof executing its statement-expression argument; only platforms that provide latest GCC/Clang would be able to use this.

    • windward an hour ago

      It is, but the bar of what's considered too 'clever' in embedded/firmware is usually lower than this. In fact, even the ternary conditional operator is too much.

    • rowanG077 an hour ago

      Definitely. I still don't think you should swim against the stream. Just bite the bullet and write idiomatic C. The people who will have to debug your code in the future will thank you.