是否可以在C中自动生成的变量名称中使用__LINE__? [英] Is it possible to use __LINE__ in the auto-generated variable name in C?

查看:160
本文介绍了是否可以在C中自动生成的变量名称中使用__LINE__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为避免重复,我想在自动生成的变量名称中使用__LINE__.

To avoid duplication, I want to use __LINE__ in the auto-generated variable name.

#define ROUTE(path, impl) \
char * k##impl##__LINE__##_route = "{"#path":\""#impl"\"}";

但始终将其视为普通字符串__LINE__.

But it always be treated as a normal string __LINE__.

即使我将其定义为以下内容,我也无法获得想要的东西:

Even if I define it as the following, I can not get what I want:

#define ROUTE(path, impl) ROUTE_(path, impl, __LINE__)

#define ROUTE_(path, impl, line) \
char * k##impl##line##_route = "{"#path":\""#impl"\"}";

推荐答案

您需要再嵌套一层:

#define ROUTE(path, impl) ROUTE_(path, impl, __LINE__)

#define ROUTE_(path, impl, line) ROUTE_1(path, impl, line)

#define ROUTE_1(path, impl, line) \
    char * k##impl##line##_route = "{"#path":\""#impl"\"}";

这篇关于是否可以在C中自动生成的变量名称中使用__LINE__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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