11

C(++) macros will be the death of me

I sure do love working with code that was written when I was in elementary school, with all the glory of nested macros and ## to deduct type names

Love that

Comments
  • 3
    Project idea: anything, but every line of code is a macro
  • 6
    @electrineer

    Someone already did this, it's called Bash
  • 0
    Macros are awesome!
    When properly documented, they are actually quite nice to understand.
  • 0
    @thebiochemic Macros are horrible

    Not seeing the same code as the compiler sees alone is just a horrible idea.

    And the worst part is using ## to append something to the macro argument.

    What I've personally seen: a C++ template that needs 4 type arguments. 4 classes that are directly related to each other, but in order to not write them out all the time there's a macro that takes the typename, and generates the template with <typename, typename##thing, typename##otherthing, ...> because the naming was consistent.
  • 0
    @LotsOfCaffeine macros don't do anything wrong, they just replace text with more text. I use it in a pretty similar way. In one of my C programs i have a constructor macro that is used similar to a struct declaration, but instead also adds all the necessary stuff to function as a list (with next pointer etc) and implements all the necessary functions for iterating and searching etc. for that list. I wanted to avoid conversion between to null* and back and also wanted to avoid writing all that list stuff more than once. And for exactly that purpose macros are perfectly fine (That is C mind you)
    I never used a lot (or such large) macros in C++ though, since most of the time it's not necessary (because of constexpr for example).

    When template stuff gets so complex, that someone saw the need to write macros for, i'm not sure if that's the macros fault.
  • 0
    @LotsOfCaffeine also, a few IDEs also check the content of macros for syntax, so it's not that bad. I think CLion did it.
  • 1
    @thebiochemic Ah well in C I feel like you have to use macros for certain things, but in C++ I don't think there are many scenarios.

    Problem is that the software I'm working with was written ages ago but is still actively used so I can't just go "aight lemme rewrite half of it"
  • 1
    @LotsOfCaffeine Had some projects where i thought the same though: why not just refactor it all, to not suck ass, when adding new stuff to it?

    I feel your pain ^^
Add Comment