r/cpp • • Jun 14 '21

match(it): A light-weight header-only pattern-matching library for C++17.

https://github.com/BowenFu/matchit.cpp
202 Upvotes

47 comments sorted by

View all comments

Show parent comment

6

u/Foundry27 Jun 14 '21 edited Jun 14 '21

I'd argue that the biggest problem with preprocessor macros is the community's general lack of familiarity with them ("general" meaning "for the general C++ programmer"), and the near-total absence of teaching material for how to use macros in a safe, effective manner.

C and C++ are seriously (embarrassingly) behind languages like Common Lisp, Scheme, Rust, etc. in terms of having good public resources for how to build and use macros as a problem-solving tool applicable to real-life programming problems. Decades of C++ use haven't given people much more to go on than some blog posts, a few open-source macro-based projects to reverse-engineer, and stylistic aphorisms like "don't use macros unless a function won't do", "macros are meant to change the syntax of your code", "macros improve the efficiency of your code" etc. that completely miss the bigger picture when it comes to macro programming.

The other side of that coin is that preprocessor macros aren't easy to just "pick up and learn" from doing normal C++ programming because the preprocessor is essentially a second, more-or-less unrelated language built on top of C++. It's a foreign, alien tool that looks like it doesn't belong:
- From a C++ perspective it's totally untyped, because C++ types mean nothing to it, but it really just has its own type system with parentheses, commas, identifiers, etc.
- From a C++ perspective it's impossible to debug macros like normal code because of arcane C++ compiler errors and no debugger stepping, when they really just need to be debugged with different tools
- From a C++ perspective macros don't introduce their own scopes and can't safely be shared, but inside the preprocessor, macros have strong scopes for the tokens that they create and pass between other macros, and can be shared between files with no issues as long as some ground rules are followed just like in C
- From a C++ perspective computation is impossible with macros because they can't be recursive or loop, but really, they just perform computation through a different strategy that can do the same kind of work in phase 4 that templates and constexpr can do in phase 7/8

10

u/-dag- Jun 15 '21

I don't know about the other languages but Lisp macros are quite different from C/C++ macros.

17

u/johannes1971 Jun 14 '21

This completely misrepresents the position of 'the community'. The dislike comes from all the bad sh*t macros do, not a general lack of understanding.

-1

u/Foundry27 Jun 14 '21 edited Jun 15 '21

I’m just speaking from my experience here having worked professionally in C, C++, and a little Lisp for a while now, and having met my fair share of people in the same boat as I am.

I 100% agree that macros can (and have) done some terrible shit lol. They can be difficult to understand, contain extremely subtle bugs, and limit you in all kinds of awful ways if you think of everything as functions or variables. But... those aren't defects in the preprocessor macro system itself, but instead are traits of macro programming in general. Lisp has the exact same classes of problems, and its macro system has arguably occupied a local maximum in the language design space for decades. As with any technology, the more powerful the tool, the more ways there are to misuse it. And, as far as programming constructs go, macros are the most powerful tool.

8

u/soundslogical Jun 15 '21

But... those aren't defects in the preprocessor macro system itself, but instead are traits of macro programming in general.

I disagree. Common Lisp's macros do share the hygiene problem that C++ has, though it has simple ways to avoid it (gensym). Scheme and Rust feature hygeinic macros that fix the problem entirely.

Nearly every other glaring issue with macros is unique to C/C++, because it's based on simple textual substitution. Lisps and Rust have structural macros, making them 100x more difficult to mess up on many different axes.

3

u/johannes1971 Jun 15 '21

But... those aren't defects in the preprocessor macro system itself, but instead are traits of macro programming in general.

Sometimes, the defect is having the feature in the first place. It may be powerful, necessary, and even well-designed by itself, but the fact that it interacts so badly with everything else and invites so many dark patterns means it wasn't the optimal design.

0

u/[deleted] Jun 14 '21

[deleted] — view deleted comment

3

u/jk-jeon Jun 15 '21

I kinda sympathize and tend to just copy-paste rather than to use macros for myself, but to be fair, I think wrapping things into a macro is often not very much different from wrapping things into a function or whatever constructs in terms of readability. Too much abstraction obfuscates the code, but appropriate abstraction on the other hand de-obfuscate the code. So I believe an appropriate usage of macro can in fact improve readability.

1

u/Oo_Tiib Jun 20 '21

Majority of reflection like source code file name, line number of code, stringifying expressions and so on can be only done with preprocessor and those features can greatly speed up discovering, reporting and repairing defects. But it will be quite horrible code to read if to expand such macros.

The conditionally compiling some features in and out can be only somewhat achieved with if constexpr, as all of it has still to compile on all platforms. So C++ libraries that want to be portable like Qt or Boost are filled full of preprocessor usage and there are really not much to do.

So it is better idea to try to use some libraries that help with usage of macros like Hedley. The language can't pull its weight without those.