lulijeta
lulijeta

Reputation: 908

Variadic function calling a variadic macro

I have an inline variadic function
inline int foo(...)
I need foo() to call a macro (let's call it MACRO), which is also variadic.
Basically I need foo() to pass all its input parameters to MACRO. Redefining foo() as another macro would be an easy solution because of __VA_ARGS__ option, but I also need foo() to return a value.
Note: I am trying to interface two parts of already written code and I am not allowed to change them. foo(...) is used in the first part of code and MACRO is defined in the second part. Only thing I am supposed to do is to define a foo() which uses MACRO and I can't because they are both variadic.

Upvotes: 8

Views: 1731

Answers (2)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275200

Make foo a macro that contains a lambda that returns a value, and then invokes that lambda.

#define foo(...) \
  [&](auto&&...args){ \
    /* do something with args, or __VA_ARGS__ */ \
    MACRO(__VA_ARGS__); \
    return 7; \
  }(__VA_ARGS__)

now int x = foo(a, b, c); will both call the lambda inside foo, and inside that lambda call the macro on (a, b, c), and can return a value.

I pity whomever maintains your code next.

Upvotes: 8

orlp
orlp

Reputation: 117641

What you're asking is impossible.

A variadic function's arguments are determined at runtime, but a macro expands at compile time.

Upvotes: 2

Related Questions