C++26: A User-Friednly assert() macro

(sandordargo.com)

37 points | by jandeboevrie 3 days ago

8 comments

  • MontagFTB 1 hour ago
    Putting code with side effects into an assert is asking for trouble. Compile with NDEBUG set and the effects mysteriously disappear! Anything beyond an equality expression or straight boolean should be avoided.
    • usrnm 19 minutes ago
      I once spent several days debugging that same mistake. Stuff worked perfectly in tests but broke misteriously in production builds. Couldn't stop laughing for a few minutes when I finally figured it out.
    • nyc_pizzadev 1 hour ago
      This is just a symptom of a bad assert() implementation, which funny enough is the standard. If you properly (void) it out, side effects are maintained.

      https://github.com/fiberfs/fiberfs/blob/7e79eaabbb180b0f1a79...

    • jmalicki 1 hour ago
      Side effects are bad of course, but anything beyond a straight boolean or equality is bad?

      `assert(vector.size() < 3)` is ridiculous to you?

    • nealabq 22 minutes ago
      I don't mean to be that guy, but for "functional" programmers a print statement has "side effects".

      But your meaning is clear. In an assert expression, don't call functions that might change the program/database state. Be as "const" as possible.

  • omoikane 1 hour ago
    > (assert) doesn't follow the usual SCREAMING_SNAKE_CASE convention we associate with macros

    There are a few things like that, for example:

    https://en.cppreference.com/w/c/numeric/math/isnan - isnan is an implementation defined macro.

    https://en.cppreference.com/w/c/io/fgetc - `getc` may be implemented as a macro, but often it's a function.

    • nealabq 28 minutes ago
      In C++ you should probably #include <cstdio> instead of <stdio.h> unless you have a good reason. And especially avoid #including both. <cstdio> provides the function std::getc(..) while <stdio.h> usually provides getc(..) as a macro.

      htons(..) and related socket-utility names are also often macros, but I'm pretty sure there is not a std::htons(..) in the C++ standard, partly because 'htons' is not an attractive name. Since it's (sometimes) a macro don't qualify its namespace like ::htons(..).

      A long time ago in the Microsoft C (and later C++) dev envs there were macros named "min" and "max", which I thought were terrible names for macros.

  • nyc_pizzadev 2 hours ago
    The nice thing about assert() is you can just define your own:

    https://github.com/fiberfs/fiberfs/blob/7e79eaabbb180b0f1a79...

    In this case, the ability to see the actual values that triggered the assert is way more helpful.

  • grokcodec 36 minutes ago
    Friedns shouldn't let Freidns post on HN without running spell check
  • amelius 55 minutes ago
    Shouldn't the preprocessor be fixed, if it trips that easily on common C++ constructs?
    • marginalia_nu 35 minutes ago
      Preprocessor is just doing text transformations on the sources.

      It's not really something that can be fixed, other than moving away from the preprocessor and putting metaprogramming capabilities into the language itself (which C++ has been doing).

    • tom_ 52 minutes ago
      I'm sure the standardization committee are always looking for fresh ideas!
  • semiinfinitely 52 minutes ago
    "C++47: Finally, a Standard Way to Split a String by Delimiter"
  • throwpoaster 56 minutes ago
    assert(spellcheck(“Friednly”));