Boost logo

Boost Users :

Subject: [Boost-users] Looping through variadic macro arguments
From: Florian Lindner (mailinglists_at_[hidden])
Date: 2016-03-18 06:19:50


Hello,

I have this macro:

#define vmacro(expr, ...) \
  cout << "n macro = " << BOOST_PP_VARIADIC_SIZE(__VA_ARGS__) << endl; \
  cout << BOOST_PP_VARIADIC_ELEM(0, __VA_ARGS__) << endl; \
  cout << BOOST_PP_VARIADIC_ELEM(1, __VA_ARGS__) << endl; \
  for (int i = 0; i < BOOST_PP_VARIADIC_SIZE(__VA_ARGS__); i++) { \
    cout << i << ": " << BOOST_PP_VARIADIC_ELEM(i, __VA_ARGS__) << endl; \
  }

The VARIADIC_SIZE is correct and I can access elements using BOOST_PP_VARIADIC_ELEM as long as I use a literal. The loop body produces error messages when compiling.
BOOST_PP_VARIADICS is 1.

The goal is to concatenate all variadic macro arguments to a string. If
 there a more clever solution, I'm very eager to hear them.

Thanks!
Florian

ERROR MESSAGES:

% clang++ -std=c++11 -g3 -rdynamic -ldl test.cpp
test.cpp:154:3: error: expected expression
  vmacro(4, 5, 6);
  ^
test.cpp:120:26: note: expanded from macro 'vmacro'
    cout << i << ": " << BOOST_PP_VARIADIC_ELEM(i, __VA_ARGS__) << endl; \
                         ^
/usr/include/boost/preprocessor/variadic/elem.hpp:26:101: note: expanded from macro 'BOOST_PP_VARIADIC_ELEM'
# define BOOST_PP_VARIADIC_ELEM(n, ...) BOOST_PP_CAT(BOOST_PP_VARIADIC_ELEM_, n)(__VA_ARGS__,)
                                                                                                    ^
1 error generated.

g++ -std=c++11 -g3 -rdynamic -ldl test.cpp
In file included from test.cpp:9:0:
test.cpp: In function 'void f(int)':
test.cpp:120:26: error: expected primary-expression before ')' token
     cout << i << ": " << BOOST_PP_VARIADIC_ELEM(i, __VA_ARGS__) << endl; \
                          ^
test.cpp:154:3: note: in expansion of macro 'vmacro'
   vmacro(4, 5, 6);
   ^
test.cpp:120:26: error: 'BOOST_PP_VARIADIC_ELEM_i' was not declared in this scope
     cout << i << ": " << BOOST_PP_VARIADIC_ELEM(i, __VA_ARGS__) << endl; \
                          ^
test.cpp:154:3: note: in expansion of macro 'vmacro'
   vmacro(4, 5, 6);
   ^


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net