0

I am trying to concatenate within a macro like this

#include <cstdio>

#include <iostream>

#define BOILERPLATE(STAGE, REQ_COMP_FUNC) \
    REQ_COMP_FUNC; \
    STAGE;

#define REGIS(...) \
    void regi() \
    { \
        APPLY_TO_VARIADIC_PAIRS_IMPL(BOILERPLATE, VA_NARGS(__VA_ARGS__), __VA_ARGS__) \
    }


#define APPLY_TO_VARIADIC_PAIRS_IMPL(func, n, ...) \
         static_assert(n==2);  \
    APPLY_TO_VARIADIC_PAIRS_IMPL_##n(func, __VA_ARGS__)

#define APPLY_TO_VARIADIC_PAIRS_IMPL_2(func, x, y) \
    func(x, y)


#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)


REGIS(std::cout << "HEY", std::cout << "HEY");



int main()
{
    
} 

As you can see I am ensuring for this simple example that my number of arguments is 2. Yet I get a compiler error with the following:

 error: use of undeclared identifier 'APPLY_TO_VARIADIC_PAIRS_IMPL_VA_NARGS'

Which for some reason means that even thoough my n is asserted to 2 when applying this operation:

APPLY_TO_VARIADIC_PAIRS_IMPL_##n(func, __VA_ARGS__)

It ends up in an unexpected form of APPLY_TO_VARIADIC_PAIRS_IMPL_VA_NARGS

Any reason and solutions for this problem

ATK
  • 1,296
  • 10
  • 26
  • 1
    What are you actually trying to achieve? Looks like a pain to debug – AndersK Feb 26 '23 at 18:07
  • Does this answer your question? [How can I concatenate twice with the C preprocessor and expand a macro as in "arg ## \_ ## MACRO"?](https://stackoverflow.com/questions/1489932/how-can-i-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-ar) – apple apple Feb 26 '23 at 18:33

0 Answers0