# C/C++图形图像的世界

• 随笔 - 2
• 文章 - 1
• 评论 - 1
• 引用 - 0

• 积分 - 8820
• 排名 - 1106

## C/C++宏的奇技淫巧

#define MACROCAT( x, y ) MACROCAT1 ( x, y )
#define MACROCAT1( x, y ) x##y
#define TOSTRING( s ) #s
MACROCAT把x和y展开后连結，而TOSTRING把s转化为字符串，比如可以printf(TOSTRING(%s), TOSTRING(abcdefg));

```#define BIN_0(arg) TOSTRING ( arg )
#define BIN_1(arg) BIN_0(MACROCAT(arg, 0)) "," BIN_0(MACROCAT(arg, 1))
#define BIN_2(arg) BIN_1(MACROCAT(arg, 0)) "," BIN_1(MACROCAT(arg, 1))
#define BIN_3(arg) BIN_2(MACROCAT(arg, 0)) "," BIN_2(MACROCAT(arg, 1))
#define BIN_4(arg) BIN_3(MACROCAT(arg, 0)) "," BIN_3(MACROCAT(arg, 1))
int main()
{
puts(BIN_4());
return 0;
}```

`"0" "0" "," "0" "1" "," "1" "0" "," "1" "1"`

#define DECVAL_1 0
#define DECVAL_2 1
#define DECVAL_3 2
#define DECVAL_4 3
#define DECVAL_5 4
#define DECVAL_6 5
#define DECVAL_7 6
#define DECVAL_8 7
#define DECVAL_9 8
#define DECVAL( n ) DECVAL_##n

```#define BIN_0(n, arg) TOSTRING ( arg )
#define BIN_1(n, arg) MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 0)) \
"," MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 1))```

source: http://blog.misakamm.org/p/209

http://blog.misakamm.org/p/209

```#define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16, N, ...) N
#define PP_RSEQ_N() \
16,15,14,13,12,11,10, \
9,8,7,6,5,4,3,2,1,0```

```#define BRACKET_L() (
#define BRACKET_R() )
#define PP_NARG(...) \
PP_NARG_ ( __VA_ARGS__, PP_RSEQ_N() )
#define PP_NARG_(...) \
PP_ARG_N BRACKET_L() __VA_ARGS__ BRACKET_R()
#define PP_ARG_N( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16, N,...) N
#define PP_RSEQ_N() \
16,15,14,13,12,11,10, \
9,8,7,6,5,4,3,2,1,0
```

vc版：
#pragma warning(disable:4003) // 去除警告

http://blog.misakamm.org/p/209

```#define BINARY_E0(n, arg) TOSTRING ( arg )
#define BINARY_E1(n, arg) MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 0) )\
"," MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 1) )
#define BINARY_E2(n, arg) MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 0) )\
"," MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 1) )
#define BINARY_E3(n, arg) MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 0) )\
"," MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 1) )
#define BINARY_E4(n, arg) MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 0) )\
"," MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 1) )
#define BINARY_E5(n, arg) MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 0) )\
"," MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 1) )
#define BINARY_E6(n, arg) MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 0) )\
"," MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 1) )
#define BINARY_E7(n, arg) MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 0) )\
"," MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 1) )
#define BINARY_E8(n, arg) MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 0) )\
"," MACROCAT(BINARY_E, DECVAL(n)) ( DECVAL(n), MACROCAT(arg, 1) )
#define BINARY_ENUM(n) MACROCAT(BINARY_E, n) ( n, )
#define BIN_0(n, arg) TOSTRING ( arg )
#define BIN_1(n, arg) MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), arg) \
"," MACROCAT(BINARY_E, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 1))
#define BIN_2(n, arg) MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), arg) \
"," MACROCAT(BINARY_E, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 1))
#define BIN_3(n, arg) MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), arg) \
"," MACROCAT(BINARY_E, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 1))
#define BIN_4(n, arg) MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), arg) \
"," MACROCAT(BINARY_E, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 1))
#define BIN_5(n, arg) MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), arg) \
"," MACROCAT(BINARY_E, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 1))
#define BIN_6(n, arg) MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), arg) \
"," MACROCAT(BINARY_E, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 1))
#define BIN_7(n, arg) MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), arg) \
"," MACROCAT(BINARY_E, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 1))
#define BIN_8(n, arg) MACROCAT(BIN_, DECVAL(n)) (DECVAL(n), arg) \
"," MACROCAT(BINARY_E, DECVAL(n)) (DECVAL(n), MACROCAT(arg, 1))
#define BIN_ENUM(n) "0" MACROCAT(BIN_, n) ( n, )

```

posted on 2012-01-16 16:22 御坂美琴 阅读(6661) 评论(0)  编辑 收藏 引用