C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Recursive macros

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Fri, 10 Nov 2023 15:37:01 +0300
On 11/10/23 15:22, Nikl Kelbon via Std-Proposals wrote:
> Hi all, I would like to propose a minor addition to the preprocessor,
> that has the potencial to significantly simplify writing and reading
> macros, give new features that were not there.
>
> I propose a special token '__THIS_MACRO__'(similar to '__VA_ARGS__' and
> '__VA_OPT__' ), which will add the possibility to write recursive macros.
>
> Simple example:
>
> #define fold_right( op , head , ...) \
> ( head __VA_OPT__ ( op __THIS_MACRO__(op, __VA_ARGS__ )))
> /* expands to (1 + (2 + (3))) */
> int i = fold_right(+ , 1 , 2 , 3 );
>
>
> This can be used in many different scenarios,
> * declaring enums with enum info(like names, count, etc)
> * boost describe struct
> * ... more examples in 'pdf' ...

When does the recursion stop? IOW, what one is supposed to write in the
macro definition to stop the recursion at a certain point?

Received on 2023-11-10 12:37:05