使用__LINE__为不同的变量名称创建宏 [英] Creating macro using __LINE__ for different variable names

查看:322
本文介绍了使用__LINE__为不同的变量名称创建宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

使用##和 LINE 创建C宏(与定位宏的令牌级联)

Possible Duplicate:
Creating C macro with ## and LINE (token concatenation with positioning macro)

我想使用 __ LINE __ 宏来生成不同的变量名。我有一个范围基准类叫Benchmark(位于utils命名空间),它的构造函数接受一个字符串。这是我创建的宏定义:

I am trying to use the __LINE__ macro to generate different variable names. I have a scoped benchmark class called Benchmark(located in the utils namespace) and it's constructor takes a string. Here is the macro definition I have created:

#define BENCHMARK_SCOPE utils::Benchmark bm##__LINE__(std::string(__FUNCTION__))

不幸的是,这会导致以下错误:

Unfortunately this causes the following error:

< some_file_name>(59):error C2374:'bm__LINE__':redefinition;多个初始化

这让我得出结论: __ LINE __ 。我已根据此信息创建了我的宏。你有什么想法 __ LINE __ 不会扩展吗?

This leads me to the conclusion the __LINE__ macros does not get expanded. I have created my macross according to this post. Do you have ideas why __LINE__ does not get expanded?

EDIT :可能编译器信息也是相关的。我使用visual studio 2010。

EDIT: probably the compiler info is also relevent. I am using visual studio 2010.

推荐答案

您需要使用两个宏的组合:

You need to use combination of 2 macros:

#define COMBINE1(X,Y) X##Y  // helper macro
#define COMBINE(X,Y) COMBINE1(X,Y)

然后使用

COMBINE(x,__LINE__);

这篇关于使用__LINE__为不同的变量名称创建宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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