添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
打酱油的啄木鸟  ·  Series — PySpark ...·  12 小时前    · 
想旅行的大蒜  ·  StellarGraph API — ...·  3 天前    · 
不羁的高山  ·  Spring Framework Bean ...·  5 天前    · 
气宇轩昂的眼镜  ·  Fastest Way to ...·  5 天前    · 
耍酷的毛豆  ·  2 containers not ...·  6 天前    · 
稳重的李子  ·  Swift if,if ... ...·  1 月前    · 

Introduction

Wouldn’t it be nice if you could define pretty-printable enums in C++? Well, in C++20, you can define a macro that both creates an enum type and defines a function converting the enum values to strings. Here’s an example of such a macro in action:

make_enum.cc. The rest of this blog post explains how to define FOR_EACH in a simple and general way. Though there’s an (arbitrary) limit to the size of the argument list, the limit is exponential in the length of the source code. With an extra 5 lines of code, you can have over 300 arguments, which seems like plenty and is way more acceptable than old approaches requiring a separate macro for each possible number of arguments.

So in this blog post, I’ll attempt to explain how C/C++ macros actually work and then show how to combine that with C++20 __VA_OPT__ to do some cool things.

C macro overview

C and C++ support two kinds of macros, object-like macros, which have no arguments, and function-like macros, which require arguments. Here are some simple examples.

trivial cpptorture.c program can require over 100 years and many exabytes of memory to compile? But I digress. More practically, people like to write code like this, from the linux <sys/epoll.h> header:

known ambiguity in the specification as to when exactly the replacing bit gets cleared. What happens if a macro expansion ends with a function-like macro, but the arguments to that macro include tokens from after the expansion? In practice, compilers seem to do the intuitive thing and clear the replacing bit exactly before the first token that entirely follows the macro expansion. For example:

Paul Fultz, is to avoid setting the unavailable bit on the macro that you want to expand recursively by hiding the token until another macro’s rescan phase.

Let’s look at an example:

trivial cpptorture.c program?), the real limit is how much time and memory we have for cpp, not the fact that cpp isn’t turing complete. Here are 5 lines of code that re-scan macros 342 times (EXPAND4 gets called 256 times, but the intermediary macros cause rescan as well):

  • language specification for macro replacement until I’d already understood how cpp works. https://en.cppreference.com/ doesn’t get into nearly the level of detail necessary. On the other hand, I now have this blog post I can reference in my source code, so writing this post is actually part of deciding whether or not I want to use the trick. Of course, others should feel free to do the same… I place all the cpp macros in this blog post in the public domain.

    FOR_EACH is also far from the grossest use of macros I’ve seen. It doesn’t even use token pasting (##) to synthesize new tokens that you can’t textually search for. Even though the implementation is tricky to understand, it’s at least short. More importantly, the interface to FOR_EACH is quite intuitive. For a multi-line C macro, I think MAKE_ENUM is fairly readable. And once you employ FOR_EACH in one place, you can potentially amortize the complexity over other uses of the macro.

    Whatever you think of the trade-offs, this much is certain: the introduction of __VA_OPT__ makes FOR_EACH