[Preprocessor] How to work around empty sequence?

I was using sequences happily until I realized that quite often I will have an empty collection. Yet empty sequences are not supported. So I switched to lists. But they are syntactically so much worse! You have to add that BOOST_PP_NIL. You have to take care of proper nesting of elements. Much less readable and editable in my opinion. This made me consider going back to sequences after all and requiring user to provide some kind of special symbol for empty sequence or end eery sequence with a fixed item so that I will now it is end and not a real item. In my opinion the extra effort would still be less than with the lists while overall "user experience" would be better. (At the expense of complicating my own code - but I'm ready to accept that.) Does anyone of you ever did something like that? Guidelines? Ideas? How do sequence function deal with empty sequences? If I would pass to BOOST_PP_SEQ_ENUM a result of BOOST_PP_SEQ_POP_FRONT on a single-element sequence would it work fine (enumerate nothing)? Or it is not guaranteed?

I was using sequences happily until I realized that quite often I will have an empty collection. Yet empty sequences are not supported. So I switched to lists. But they are syntactically so much worse! You have to add that BOOST_PP_NIL. You have to take care of proper nesting of elements. Much less readable and editable in my opinion.
Also lists are much slower than sequences.
This made me consider going back to sequences after all and requiring user to provide some kind of special symbol for empty sequence or end eery sequence with a fixed item so that I will now it is end and not a real item. In my opinion the extra effort would still be less than with the lists while overall "user experience" would be better. (At the expense of complicating my own code - but I'm ready to accept that.) Does anyone of you ever did something like that? Guidelines? Ideas? How do sequence function deal with empty sequences? If I would pass to BOOST_PP_SEQ_ENUM a result of BOOST_PP_SEQ_POP_FRONT on a single-element sequence would it work fine (enumerate nothing)? Or it is not guaranteed?
You can make `BOOST_PP_SEQ_ENUM` work on empty sequences by defining `#define BOOST_PP_SEQ_ENUM_0`. Also, `BOOST_PP_SEQ_FOR_EACH` should work on empty sequences. However `BOOST_PP_SEQ_FOLD_LEFT` won't work. And `BOOST_PP_SEQ_FILTER` and `BOOST_PP_SEQ_TRANSFORM` relies on `BOOST_PP_SEQ_FOLD_LEFT`, so those won't work either. Sequence iteration will work on empty sequences(like I showed in another email), it just requieres a little bit more of boilerplate: #define OP(...) ... #define TRANSFORM(x) BOOST_PP_CAT(TRANSFORM_0 x, _END) #define TRANSFORM_0(...) (OP(__VA_ARGS__)) TRANSFORM_1 #define TRANSFORM_1(...) (OP(__VA_ARGS__)) TRANSFORM_0 #define TRANSFORM_0_END #define TRANSFORM_1_END Also, if you start using `BOOST_PP_SEQ_FOR_EACH` everywhere you may run into an issue with calling them recursively(especially if you have nested sequences). Unfortunately, `BOOST_PP_SEQ_FOR_EACH` is not reentrant, but there are workarounds so they can be called recursively, however. Just let me know if you run into this. Paul
participants (2)
-
Adam Badura
-
paul Fultz