lefticus / cpp_weekly

The official C++ Weekly Repository. Code samples and notes of future / past episodes will land here at various times. PR's will be accepted in some cases.
The Unlicense
710 stars 26 forks source link

Macros in metaprogramming #444

Open Eczbek opened 5 days ago

Eczbek commented 5 days ago

Channel This is a C++Weekly episode request, or perhaps just an interesting idea to mention.

Topics Macros in metaprogramming! Some common ones like FWD, ARROW, and LIFT: https://godbolt.org/z/5dbqqv8x8

Length Not sure how much this topic can be explored

LB-- commented 1 day ago

Why does the FWD macro use VA_ARGS when it's not possible to use it with any number of arguments other than 1?

Also, major compilers recently got optimizations for std::forward and std::move to behave just like the cast even in unoptimized debug builds, so there's less of a need for such a macro.

In my own code, I only use macros for excessively repetitive code to make it easier to keep the actual differences in view and aligned. When it comes to just saving typing like in the examples in that link, I find macros make it more difficult to read the code.

Eczbek commented 1 day ago

@LB--

Why does the FWD macro use VA_ARGS when it's not possible to use it with any number of arguments other than 1?

The macro must accept any number of arguments because the preprocessor doesn't recognize <> and [], e.g. something like foo<int, int> would actually count as 2 arguments.

Also, major compilers recently got optimizations for std::forward and std::move to behave just like the cast even in unoptimized debug builds

True, but the point of this macro is to make forwarding less verbose. With the FWD macro you don't need to pass both a type and a value, instead you just pass the value. I suppose this aspect could be up to preference.