Boost logo

Boost Users :

Subject: Re: [Boost-users] Preprocessor: Expaning empty VA_ARGS
From: niXman (i.nixman_at_[hidden])
Date: 2016-09-08 11:01:21


Florian Lindner 2016-09-08 17:35:
> Hello,
Hi,

> I have this preprocessor macros:
>
> -----------
>
> #include <boost/preprocessor/variadic/to_seq.hpp>
> #include <boost/preprocessor/seq/for_each_i.hpp>
> #include <boost/preprocessor/stringize.hpp>
>
> #include <iostream>
>
> #define TRACE(...) \
> std::cout << "foo"
> \
> BOOST_PP_SEQ_FOR_EACH_I(LOG_ARGUMENT,,
> BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__));
>
> #define LOG_ARGUMENT(r, data, i, elem)
> \
> << std::endl << " Argument " << i << ": " <<
> BOOST_PP_STRINGIZE(elem) << " == " << elem
>
>
> int main(int argc, char *argv[])
> {
> TRACE();
>
> return 0;
> }
>
> -----------
>
> They compile when TRACE has arguments, but not without. Problem is,
> that LOG_ARGUMENT is called even when no args are
> supplied, leading to the expansion:
>
> std::cout << "foo" << std::endl << " Argument " <<
> 0
> << ": " << "" << " == " << ;;
>
> elem is empty, therefore there is no expression after the last <<
>
> expected primary-expression before »;« token
>
> How can I deal with that?

You need to check if '__VA_ARGS__' is not empty using
'BOOST_VMD_TUPLE_SIZE()'[1] or something like this[2][3].

[1]
http://www.boost.org/doc/libs/1_61_0/libs/vmd/doc/html/BOOST_VMD_TUPLE_SIZE.html
[2]
https://github.com/niXman/nanolog/blob/master/include/nanolog/nanolog.hpp#L97
[3]
https://github.com/niXman/nanolog/blob/master/include/nanolog/nanolog.hpp#L591

-- 
Regards, niXman
___________________________________________________
Dual-target(32 & 64-bit) MinGW-W64 compilers for 32 and 64-bit Windows:
http://sourceforge.net/projects/mingw-w64/

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