How does BOOST_PP_SEQ_SIZE works?

Viewed 132
1 Answers

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.

Related