cpp_weekly icon indicating copy to clipboard operation
cpp_weekly copied to clipboard

Macros in metaprogramming

Open Eczbek opened this issue 1 year ago • 2 comments

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

  • Is it good/bad to use helpers like this?
  • Do there exist any other macros of similar nature?

Length Not sure how much this topic can be explored

Eczbek avatar Nov 28 '24 17:11 Eczbek

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.

LB-- avatar Dec 03 '24 00:12 LB--

@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.

Eczbek avatar Dec 03 '24 02:12 Eczbek