0

Not complicated way to overload MACROS with null,one,two arguments

based on this comment I want to overload one/two arguments and if possible 0 argument, but with minimal rows of macros.

Now I did in the following mode

// For 1..2 arguments, we get 4 rows
#define GET_MACRO(_1,_2, NAME,...) NAME
#define FOO(...) GET_MACRO(__VA_ARGS__, FOO2, FOO1)(__VA_ARGS__)
#define FOO1(x) ABC(F(x))
#define FOO2(x,y) ABC(F(x), F(y))

FOO(x)  ->  ABC(F(x))

but I have two questions

  1. is it possible to do this with less then 4 rows ?
  2. is it possible to make overload for 0 arg in the following mode

    FOO() -> ABC()
    

Update: 2nd question is not actual any more. So now I have for 0..2 arg - 5 rows. For 1..2 arg - 4 rows. So each additional func_n_arg() need to have separate converter + 2 rows for counting arguments. Is it possible to do with less rows of macros? for the same thing

  • Do you think four rows is very much? And did the answers that question not address the case of 0 args? – L. F. Jun 22 '19 at 14:08
  • 1
    Possible duplicate of [Overloading Macro on Number of Arguments](https://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments) – L. F. Jun 22 '19 at 14:08
  • @L. F.
    My four rows is not include 0 arg.
    If possible to do the same thing with 3 rows why we should use 4 rows method :)
    – Alex Blade Jun 23 '19 at 05:39
  • @L. F. and its not duplicate of *your link* its based on. I mentioned this in first post. In this link author only want to overload to many different functions. In my case I need only one function but with different number of arguments AND with minimal count of macro rows Sorry for my english and Im not professional programmer – Alex Blade Jun 23 '19 at 05:44
  • If you want to handle 0..N arguments, you need N+1 macros with a numeric suffix and 2 more to handle the counting and provide the generic interface. You can't get it smaller than that. Why do you think the number of macros is a problem? Unless you start going up to hundreds of arguments, it isn't going to stress a compiler (but it might stress you as the author). And hundreds of them probably isn't going to cause the compiler much problem. – Jonathan Leffler Jun 23 '19 at 11:12
  • @JonathanLeffler yeh, its stress for author from one side, from other I expect small code for small task :) sorry for my english – Alex Blade Jul 03 '19 at 19:27

0 Answers0