According to https://www.boost.org/doc/libs/1_68_0/libs/preprocessor/doc/ref/seq_size.html:
BOOST_PP_SEQ_SIZE macro expands to the size of a seq.
Sample Code
#include <boost/preprocessor/seq/size.hpp> #define SEQ (a)(b)(c) BOOST_PP_SEQ_SIZE(SEQ) // expands to 3
This is magical. How does it work?
You can read the source here: https://www.boost.org/doc/libs/1_75_0/boost/preprocessor/seq/size.hpp
There are versions for different compilers, but not much difference. The definition of this macro looks like:
# define BOOST_PP_SEQ_SIZE(seq) BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_0 seq)
# define BOOST_PP_SEQ_SIZE_0(_) BOOST_PP_SEQ_SIZE_1
# define BOOST_PP_SEQ_SIZE_1(_) BOOST_PP_SEQ_SIZE_2
# define BOOST_PP_SEQ_SIZE_2(_) BOOST_PP_SEQ_SIZE_3
# define BOOST_PP_SEQ_SIZE_3(_) BOOST_PP_SEQ_SIZE_4
# define BOOST_PP_SEQ_SIZE_4(_) BOOST_PP_SEQ_SIZE_5
... ...
# define BOOST_PP_SEQ_SIZE_256(_) BOOST_PP_SEQ_SIZE_257
# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_0 0
# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_1 1
# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_2 2
... ...
# define BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_257 257
This piece of code:
#define SEQ (a)(b)(c)
BOOST_PP_SEQ_SIZE(SEQ)
will be expanded into:
BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_0 (a)(b)(c))
-> BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_1 (b)(c))
-> BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_2 (c))
-> BOOST_PP_CAT(BOOST_PP_SEQ_SIZE_, BOOST_PP_SEQ_SIZE_3)
-> BOOST_PP_SEQ_SIZE_BOOST_PP_SEQ_SIZE_3
-> 3
That's how it works.
A point to note is that there is not a friendly error message prepared for the situation when the sequence is longer than expected (e.g. 257), which, however, exists in the for
section of boost/preprocessor
.