
On 3/2/2011 4:11 AM, Nathan Ridge wrote:
Hello,
I would like to write a macro MY_MACRO(x) that does different things based on the structure of its input, e.g. something different for each of the following forms:
MY_MACRO(x) // a single token MY_MACRO((x, y)) // a parenthesized list of two tokens MY_MACRO((x, y, z)) // a parenthesized list of three tokens MY_MACRO(x y) // two tokens MY_MACRO(x y z) // three tokens MY_MACRO(x = y) // three tokens with the second one being something specific
Are any of these possible?
I will assume your examples are numbered from 1 to 6.
In numbers 2 and 3 the parameter is a pp-lib tuple. Normally you would
have to know in advance how many elements are in a tuple but if your
compiler supports variadic macros you can use my variadic_macro_data
library in the sandbox to know how many elements are in the pp-lib tuple
and then you can subsequently extract any one of them.
Numbers 1,4,5,6 are single elements. There is no way of which I know to
determine how many space separated sequences of text there are in each
element or extract any one of them. But there is a way to check whether
the sequence starts with a given sequence, and you may be able to work
from there via Lorenzo Caminiti's clever example, which he worked out
for his Local library:
"NOTE: This is more of a curiosity because for my application I can
manipulate the storage classifiers using preprocessor metaprogramming.
In fact, `auto` and `register` are known alphanumeric tokens (i.e.,
keywords) that always appear at the beginning of the parameter type
expression so the following macros can be used to manipulate them:
#include