Login
You're viewing the fosstodon.org public feed.
  • Nov 21, 2026, 10:41 AM

    @barubary I once experimented with a macro called 'since', which looks like a control flow statement, and has the semantics that it checks its condition by assert() and then unconditionally executes the body.

    if (x > 0) {
    // do this
    } else if (x == 0) {
    // do that
    } else since (x < 0) {
    // do the other
    }

    💬 1🔄 3⭐ 15

Replies

  • 💬 1🔄 0⭐ 0
  • 💬 1🔄 0⭐ 2
  • Nov 21, 2026, 10:59 AM

    @barubary @mirabilos yes, I think that was more or less what I did, although I might have been on the ball enough to avoid leaving the if unbalanced:

    # define since(condition) if (assert(condition), 0); else

    But I didn't have to go as far as the alternative technique I invented much later, which would have looked something like this:

    # define since(condition) \
    if (1) { \
    assert(condition); \
    goto MAKE_LABEL; \
    } else \
    MAKE_LABEL:

    where MAKE_LABEL in turn creates a label name by token-pasting on to __LINE__, so that you can do this more than once in a function without getting duplicate label errors.

    (Doing it this way you wouldn't be allowed to write more than one 'since' on the same source line. Probably nobody cares, but it is a weakness of this technique in principle. The simpler approach with the comma operator is better, in cases where it works.)

    💬 2🔄 0⭐ 3
  • 💬 1🔄 0⭐ 0
  • 💬 2🔄 0⭐ 0
  • Nov 21, 2026, 11:17 AM

    @mirabilos @barubary it's in gcc 13.3, on my Ubuntu 24.04 system.

    If I remember rightly, __COUNTER__ was originally an MSVC-ism. But of course any compiler with a sufficiently thorough MSVC compat mode would have to implement it for that mode. And then they might decide to turn it on everywhere, on the principle that it's easy, compliant (we're in the __ namespace) and might well come in useful.

    I've occasionally wondered about changing over the case-generation macros in my coroutine system to use it (where available), eliminating the restriction about two yields on the same line, and also packing the case values more tightly so that the compiler is more likely to be able to generate an efficient dispatch table. But eliminating that restriction is a double-edged sword, because if not _all_ C compilers support __COUNTER__, it puts you at more risk of writing code that works for you but not for everyone else who needs it.

    And __COUNTER__ still isn't in standard C, as of C23.

    💬 1🔄 0⭐ 4
  • 💬 0🔄 0⭐ 0
  • 💬 1🔄 0⭐ 1
  • Nov 21, 2026, 11:35 AM

    @simontatham @barubary 3.4, any newer is not a good fit for the base system because if licence reasons and because it makes the boot floppy not fit any more and because of slop, plus this one works well for gpc

    💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0