Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macros in metaprogramming #444

Open
Eczbek opened this issue Nov 28, 2024 · 2 comments
Open

Macros in metaprogramming #444

Eczbek opened this issue Nov 28, 2024 · 2 comments

Comments

@Eczbek
Copy link

Eczbek commented Nov 28, 2024

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

@LB--
Copy link

LB-- commented Dec 3, 2024

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
Copy link
Author

Eczbek commented Dec 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants