I am trying to write code for a macro that returns the length of a string, and I am trying to implement it with BOOST_PP_WHILE. The code stems from the fact that the character at the position given by the position string represented by the macro argument foo can be obtained using #foo[position] . Compilation using MSVC or Intel C ++ leads to similar syntax errors; if you could indicate why the code generates these syntax errors and how I will correct the code, it would be very useful. I know that errors are caused by code inside the PREDICATE macro, but any expression that I try to use in it that prohibits BOOST_PP_TUPLE_ELEM leads to a compile-time error.
Error:
prog.cpp:47:1: error: pasting "BOOST_PP_BOOL_" and ""\"Hello, World!\""" does not give a valid preprocessing token prog.cpp: In function 'int main(int, char**)': prog.cpp:47: error: 'BOOST_PP_TUPLE_ELEM_2_1' was not declared in this scope
As you would expect, line numbers are not very useful, since both point to the line on which the MACRO_STRLEN macro is MACRO_STRLEN .
code
The following is a list of sources in which I am trying to implement the macro that I am describing.
#include <boost/preprocessor/arithmetic/dec.hpp> #include <boost/preprocessor/arithmetic/inc.hpp> #include <boost/preprocessor/comparison/equal.hpp> #include <boost/preprocessor/control/while.hpp> #include <boost/preprocessor/tuple/elem.hpp> #include <cstdio> #define TEST_STRING0 "Hello, World!" #define MACRO_IS_NULL_IMPL(x, position) \ #x[position] == '\0' #define MACRO_IS_NULL(x, position) \ MACRO_IS_NULL_IMPL(x, position) #define PREDICATE_D(string, position) \ MACRO_IS_NULL(string, position) #define PREDICATE(n, state) \ PREDICATE_D( \ BOOST_PP_TUPLE_ELEM(2, 0, state), \ BOOST_PP_TUPLE_ELEM(2, 1, state) \ ) #define OPERATION_D(string, position) \ ( \ string, \ BOOST_PP_INC(position) \ ) #define OPERATION(d, state) \ OPERATION_D( \ BOOST_PP_TUPLE_ELEM(2, 0, state), \ BOOST_PP_TUPLE_ELEM(2, 1, state) \ ) #define MACRO_STRLEN_IMPL(string) \ BOOST_PP_TUPLE_ELEM( \ 2, 1, BOOST_PP_WHILE(PREDICATE, OPERATION, (string, 0)) \ ) #define MACRO_STRLEN(string) \ MACRO_STRLEN_IMPL(string) int main(int argc, char ** argv) { printf("String length: %d.\n", MACRO_STRLEN(TEST_STRING0)); return 0; }