用于将整数映射到字符串的宏 [英] Macro for mapping integer to string for token

查看:280
本文介绍了用于将整数映射到字符串的宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个参数 N TAG(N,C) > C 生成TAG_ + f(N)+ g(C)形式的令牌,其中 f(1)= A f(2)= AB f(3)= ABC f g(0)=(空字符串) g(1)= _FOO g(2)= _BAR

I need a macro TAG(N,C) which takes to arguments N and C generates a token of the form TAG_ + f(N) + g(C), where f(1) = A, f(2) = AB, f(3) = ABC, f(4) = ABCD and g(0) = (empty string), g(1) = _FOO, g(2) = _BAR.

示例:

TAG(3,0) -> TAG_ABC
TAG(2,1) -> TAG_AB_FOO
TAG(4,2) -> TAG_ABCD_BAR

这是最好的方法是什么?

What is the best way to do this?

推荐答案

您可以使用以下方式:

#define f_1 A
#define f_2 AB
#define f_3 ABC
#define f(N) f_##N

#define g_0 /* Empty */
#define g_1 _FOO
#define g_2 _BAR
#define g(N) g_##N

#define CONCAT3_(A, B, C) A ## B ## C
#define CONCAT3(A, B, C) CONCAT3_(A, B, C)
#define TAG(N, C) CONCAT3(TAG_, f(N), g(C))

这篇关于用于将整数映射到字符串的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆