Hacker News new | ask | show | jobs
by 0c3ca83 45 days ago
Doing non-trivial preprocessing is a mistake. You get slow compilation, hard to debug code, opaque and fragile code, and incredible complexity. If you want metaprogramming, you're better off with C++ it you need C header compatibility, or zig if you don't.

Otherwise, it's probably a good idea to pretend that the only preprocessing directive is #include. With constexpr, even #define is obsolescent.

3 comments

Doing non-trivial anything you slow down compilation, make debugging harder and increase fragility and complexity. Sometimes that's a mistake, sometimes not.

Getting rid of something because someone sometimes abuses it, is short-sighted.

You don't need to get rid of it to leave it unused. Macro wizardry leads to poor code.
Yes and no. If you want to make something like a generic dynamic array, you can do it with type erasure (`void*`), but then your code is no longer typesafe. I would find that more fragile. The preprocessor allows you to add constructs that make it behave like any other modern language.

As an aside, clangd can dereference a macro in place to make debugging easy.

And for generic data structures one can also a very thin layer of macros around type-erased implementations.
You're usually better off just using realloc, or an abort-on-oom wraper of it, in my experience.
Okay smarty-pants. What if I want a generic map then ;)
> slow compilation, hard to debug code, opaque and fragile code, and incredible complexity

Most people describe C++ in those terms.

Exactly. So if you're willing to accept that downside, C++ does a better job of letting you express that kind of cleverness.
I am not sure. I agree one should minimize macros in C, but for some things a small macro layer still beats the complexity of C++ and is not inferior in my opinion.
C++ templates are far cleaner, easier to debug, and produce more comprehensible errors than C macros. And C++ templates are rightly famous for being terrible at all of the above.
Hey, I agree about your second sentence but I do not really see the first. Alone that I can directly inspect the result of the preprocessor makes macros easier to debug than templates in my experience.
What you're saying is like saying "C can be expanded to assembly, and that makes it easier to debug". If you need to do that, the entire process has failed.
> I can directly inspect the result of the preprocessor makes macros easier to debug

Tangential note but: why does this require a separate re-invocation of the compiler with the '-E' flag? Say I have this silly example with a comma instead of the semicolon inside W() macro:

    #define W(a, b)  b, int a
    #define X(a, b)  W(b, a)

    int kekw(int x) {
        int z = X(23, k) = x;
        return z + k;
    }
When I shove it into GCC without -E, it says

    <source>: In function 'int kekw(int)':
    <source>:1:21: error: expected unqualified-id before 'int'
        1 | #define W(a, b)  b, int a
          |                     ^~~
    <source>:2:18: note: in expansion of macro 'W'
        2 | #define X(a, b)  W(b, a)
          |                  ^
    <source>:5:13: note: in expansion of macro 'X'
        5 |     int z = X(23, k) = x;
          |             ^
    <source>:6:16: error: 'k' was not declared in this scope
        6 |     return z + k;
          |       
instead of, you know, showing me the expanded text so that I could see what's actually wrong with the generated variable declarations? Something like

    <source>: In function 'int kekw(int)':
    <source>:1:21: error: expected unqualified-id before 'int'
        1 | #define W(a, b)  b, int a
          |                     ^~~
    <source>:2:18: note: in expansion of macro 'W'
        2 | #define X(a, b)  W(b, a)
          |                  ^
    <source>:5:13: note: in expansion of macro 'X'
        5 |     int z = X(23, k) = x;
          |             ^
    <source>:5:-: note: the expanded source text is
          |     int z = 23, int k = x;
          |                 ^~~
    <source>:6:16: error: 'k' was not declared in this scope
        6 |     return z + k;
          |       
perhaps? I understand that tracking the lines/columns inside the expanded text is highly non-trivial (although collapsing them all onto the position of the top-level macro is already just as non-trivial), but still. Show me what the source that you actually can't compile, not the three-steps removed precursor to it — I sure as hell can't expand macros in my head.

Also, what is this first error message about in the first place? Why the hell does it expect unqualified-id before "int"? If you put "int" after the unqualified-id, it still would be illegal; and an unqualified-id is not the only thing that can follow the comma in this context anyway.