I have a macro to convert a string to a list of characters:
#define TO_STRING(x) #x
#define CHAR_LIST_7(x) TO_STRING(x)[0] \
, TO_STRING(x)[1] \
, TO_STRING(x)[2] \
, TO_STRING(x)[3] \
, TO_STRING(x)[4] \
, TO_STRING(x)[5] \
, TO_STRING(x)[6]
e.g. usage:
“CHAR_LIST_7(chicken)” gives “‘c’, ‘h’, ‘i’, ‘c’, ‘k’, ‘e’, ‘n'” so it can be used in things like templates (e.g.: http://hpaste.org/47313/exand )
However, I would like to generalize this for any amount of characters (and not need to manually have to count the amount of characters)? So I could simply go: CHAR_LIST(arbitrary text). Any ideas or solutions ?
You can’t split tokens during preprocessing, you can only combine them (using
##).Converting the identifier to a string literal won’t help either as you can’t string split a string literal apart during preprocessing, nor can you perform operations (e.g. compute length) on a string literal.
During preprocessing the compiler knows that a token is a string literal and what kind of literal it is, but it does not yet know its full type and length, at least not in a way that is accessible to a macro.