Boost logo

Boost Users :

Subject: Re: [Boost-users] Looping through variadic macro arguments
From: Edward Diener (eldiener_at_[hidden])
Date: 2016-03-23 11:56:25

On 3/23/2016 11:45 AM, Florian Lindner wrote:
> Hello,
> another question: I was expecting that the line
> in a macro #define vm(expr, ...) expands to nothing, when the the macro is called with just one argument vm(false), but it fails, only when called with just one argument:
> % g++ -std=c++11 -g3 test.cpp
> test.cpp: In function 'void f(int)':
> test.cpp:109:51: error: expected primary-expression before '<<' token
> std::cerr << " Argument " << i << ": " << elem << std::endl;
> ^
> /usr/include/boost/preprocessor/seq/for_each_i.hpp:85:66: note: in expansion of macro 'PRINT_ARGUMENT'
> # define BOOST_PP_SEQ_FOR_EACH_I_M_I(r, macro, data, seq, i, sz) macro(r, data, i, BOOST_PP_SEQ_HEAD(seq))
> ^
> test.cpp:140:1: note: in expansion of macro 'assertion'
> assertion(!true);
> ^
> Is there any way to make it work like that?

When you specify '...' to indicate variadic arguments you must always
supply at least one argument. That's part of the C++ standard.

The way to make 'vm' work is to specify:

#define vm(...)

and then extract your first argument and pass the remaining arguments,
if they exist, to your BOOST_PP_SEQ_FOR_EACH_I expansion. I will let you
figure out how to do that, but if you find you can't figure it out post
back and I will show you the code.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at