-2

C++ gurus,

I have a template metaprogramming question. Consider the following function declaration.

int foo(const int x[], char *y, size_t size);

I would like to be able to generate the function body as follows:

int foo(const int x[], char *y, size_t size)
{
  return myfnc(x, y, size);
}

Is there a way to do this with C++ template metaprogramming? I can "wrap" the declaration in some macro, but I'd like to avoid the use of macros to do the full expansion. For example, I'd be happy if I can write something along the lines of:

DEFINE_FNC(foo, const int x[], char *y, size_t size)

where DEFINE_FNC is defined as

#define DEFINE_FNC(fnc, ...) \
int fnc(__VA_ARGS__) {       \
  return myfnc(__VA_ARGS__); \ // This doesn't work; is there something else that can be done here?
}                            \

I'm aware of the solution mentioned here, but that requires me to add parenthesis around the parameter types, and I'd like to avoid that, if possible.

I tried to use variadic templates, but it doesn't do the exact thing I want.

userRG
  • 99
  • 7
  • 2
    An XY problem? Explain what you are actually trying to accomplish. –  Aug 25 '18 at 23:22
  • "that requires me to add parenthesis around the parameter types," ...how else do you expect the preprocessor to get at the `x` in `const int x []`? (In C++, you really should prefer the variadic template approach anyway). – H Walters Aug 26 '18 at 01:59

1 Answers1

0
template <typename Args...>
int foo(Args&&... args) {
  return myfnc(std::forward<Args>(args)...);
}

You can have a macro stamping out such definitions, if you are so inclined.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85