我尝试没有任何结果.我的代码看起来像这样:
#include "stdafx.h"
#include <iostream>
#define R() ( rand() )
#define H(a,b) ( a ## b )
#define S(a) ( # a )
#define CAT() H(S(distinct_name_), R())
int main(int argc, _TCHAR* argv[])
{
std::cout << CAT() << std::endl;
std::cout << CAT() << std::endl;
std::cout << CAT() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想得到这样的结果:
distinct_name_12233
distinct_name_147
distinct_name_435
as a result of concatenating
distinct_name_ (##) rand()
Run Code Online (Sandbox Code Playgroud)
现在我收到一个错误:term不会计算为带有1个参数的函数.这可以实现吗?
编辑:几个小时后我终于成功了.预处理器仍然做我完全无法理解的奇怪事情.在这里:
#include "stdafx.h"
#include <iostream>
class profiler
{
public:
void show()
{
std::cout << "distinct_instance" << std::endl;
}
};
#define XX __LINE__
#define H(a,b) ( a ## b )
#define CAT(r) H(distinct_name_, r)
#define GET_DISTINCT() CAT(XX)
#define PROFILE() \
profiler GET_DISTINCT() ;\
GET_DISTINCT().show() ; \
int main(int argc, _TCHAR* argv[])
{
PROFILE()
PROFILE()
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
distinct_instance
distinct_instance
Run Code Online (Sandbox Code Playgroud)
谢谢@Kinopiko __LINE__提示.:)
| 归档时间: |
|
| 查看次数: |
1207 次 |
| 最近记录: |