## 在C++定义代码中的含义是什么
What's the meaning of ## in the define code in C++
#define DECLARE_DELETE_PTR(type)
void DeletePtr_##type(string &operand)
{
}
c++中的宏定义中##
的含义是什么?
与后面的代码有什么不同?
#define MAKE_STRINGS(VAR) #VAR
只有一个#,但前者是两个#
它要求预编译器连接两个令牌。
#define DECLARE_DELETE_PTR(type)
void DeletePtr_##type(string &operand)
{
}
DECLARE_DELETE_PTR(int)
将得到:
void DeletePtr_int(string &operand)
//^^^ the macro argument is int, so the precompiler replaces it here
{
}
实际上,在宏代码中,参数type
与命令的其余部分连接在一起。如果宏参数是int
,那么它只是一个简单的替换,给出上面的结果。请记住,因为它是一个预处理器指令,所以它完全发生在编译时。
如果你在linux上,我建议你看一下cpp
命令,以便更好地理解。
至于你的第二个问题,区别在于它只是两个不同的操作符。
顾名思义->它将其参数转换为c-string(我刚刚尝试过)。例如:
std::cout << MAKE_STRINGS(Hello World) << std::endl;
会变成:
std::cout << "Hello World" << std::endl
或者,更有趣的:
std::cout << MAKE_STRINGS("Hello" World) << std::endl;
就变成:
std::cout << ""Hello" World" << std::endl;
它似乎也照顾转义特殊字符,但我可能是错的-来自3分钟前的实验
它将您通过参数type
传递的值连接在一起…
DECLARE_DELETE_PTR(gremlin)
展开为:
void DeletePtr_gremlin(string &operand)
{
}
##操作符用于连接两个令牌。下面是一个例子:
DECLARE_DELETE_PTR(MyType)
//展开为
void DeletePtr_MyType(string &operand)
{
}
相关文章:
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 以下 C++ 代码用于 -> "#define idiv(a, b) (((a) + (b) / 2) / (b))" 是什么?
- 此代码中的操作流程是什么?C/C++.
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 此代码验证公式是什么意思?
- 这行代码到底是什么意思?
- 在nodejs中使用本机代码的最佳方法是什么?
- 无法访问的代码 在 C++11 中,可能导致这种情况的原因是什么?
- 以下与指针相关的代码的输出是什么?
- 这是什么代码?为什么它有效?C++
- 此代码中的" "是什么,它的基本作用是什么?
- 使用 gtest 框架在单元测试代码中检查目标对象的私有变量的最佳实践是什么?
- 当我选择大于 720 的矩阵大小时,程序退出并显示错误代码.可能是什么原因?
- 我的代码中针对 NQueens 问题的错误是什么?
- 我在 2D 数组的动态内存分配中遇到了一些奇怪的代码C++? 请解释一下这是什么?
- 这段代码"seperature | | i"是什么意思?
- 这行代码在C++中的含义是什么?
- 这是什么代码?C或C