/ 猿问

# 我们可以有递归宏吗？

2019-09-21 11:19:19

# define pr(n) ((n==1)? 1 : pr(n-1))

void main ()

{

int a=5;

cout<<"result: "<< pr(5) <<endl;

getch();

}

## 3 回答

HUWWW

> g++ -E recursiveMacro.c

# 1 "recursiveMacro.c"

# 1 "<built-in>"

# 1 "<command line>"

# 1 "recursiveMacro.c"

void main ()

{

int a=5;

cout<<"result: "<< ((5==1)? 1 : pr(5 -1)) <<endl;

getch();

}

pr(5)

^

((5==1)? 1 : pr(5-1))

^

# define EMPTY(...)

# define DEFER(...) __VA_ARGS__ EMPTY()

# define OBSTRUCT(...) __VA_ARGS__ DEFER(EMPTY)()

# define EXPAND(...) __VA_ARGS__

# define pr_id() pr

# define pr(n) ((n==1)? 1 : DEFER(pr_id)()(n-1))

pr(5) // Expands to ((5==1)? 1 : pr_id ()(5 -1))

EXPAND(pr(5)) // Expands to ((5==1)? 1 : ((5 -1==1)? 1 : pr_id ()(5 -1 -1)))

EXPAND(EXPAND(pr(5))) // Expands to ((5==1)? 1 : ((5 -1==1)? 1 : ((5 -1 -1==1)? 1 : pr_id ()(5 -1 -1 -1))))

#define EVAL(...)  EVAL1(EVAL1(EVAL1(__VA_ARGS__)))

#define EVAL1(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))

#define EVAL2(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))

#define EVAL3(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))

#define EVAL4(...) EVAL5(EVAL5(EVAL5(__VA_ARGS__)))

#define EVAL5(...) __VA_ARGS__

#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)

#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__

#define INC(x) PRIMITIVE_CAT(INC_, x)

#define INC_0 1

#define INC_1 2

#define INC_2 3

#define INC_3 4

#define INC_4 5

#define INC_5 6

#define INC_6 7

#define INC_7 8

#define INC_8 9

#define INC_9 9

#define DEC(x) PRIMITIVE_CAT(DEC_, x)

#define DEC_0 0

#define DEC_1 0

#define DEC_2 1

#define DEC_3 2

#define DEC_4 3

#define DEC_5 4

#define DEC_6 5

#define DEC_7 6

#define DEC_8 7

#define DEC_9 8

#define CHECK_N(x, n, ...) n

#define CHECK(...) CHECK_N(__VA_ARGS__, 0,)

#define NOT(x) CHECK(PRIMITIVE_CAT(NOT_, x))

#define NOT_0 ~, 1,

#define COMPL(b) PRIMITIVE_CAT(COMPL_, b)

#define COMPL_0 1

#define COMPL_1 0

#define BOOL(x) COMPL(NOT(x))

#define IIF(c) PRIMITIVE_CAT(IIF_, c)

#define IIF_0(t, ...) __VA_ARGS__

#define IIF_1(t, ...) t

#define IF(c) IIF(BOOL(c))

#define EAT(...)

#define EXPAND(...) __VA_ARGS__

#define WHEN(c) IF(c)(EXPAND, EAT)

#define REPEAT(count, macro, ...) \

WHEN(count) \

( \

OBSTRUCT(REPEAT_INDIRECT) () \

( \

DEC(count), macro, __VA_ARGS__ \

) \

OBSTRUCT(macro) \

( \

DEC(count), __VA_ARGS__ \

) \

)

#define REPEAT_INDIRECT() REPEAT

//An example of using this macro

#define M(i, _) i

EVAL(REPEAT(8, M, ~)) // 0 1 2 3 4 5 6 7

C ++标准第16.3.4节第2段中的相关语言：

• 3 回答
• 0 关注
• 77 浏览

0/150