From #define to function
From #define to function
我在函数中有这段代码,但我不明白它的作用。
....
#define ascend(i) do {
int h = nodes[i].heavyindex;
int p = nodes[i].heavypos;
m##i = max(m##i + paths[h].ftree.sum(p), paths[h].stree.max_(0, p));
i = paths[h].parent;
} while (0)
while (nodes[a].heavyindex != nodes[b].heavyindex) {
if (nodes[a].heavyindex > nodes[b].heavyindex) {
ascend(a);
} else {
ascend(b);
}
}
#undef ascend
...
我认为#define
的代码是:
#define ascend(i) do {
int h = nodes[i].heavyindex;
int p = nodes[i].heavypos;
m##i = max(m##i + paths[h].ftree.sum(p), paths[h].stree.max_(0, p));
i = paths[h].parent;
} while (0)
所以函数内部的真正代码只有这个:
while (nodes[a].heavyindex != nodes[b].heavyindex) {
if (nodes[a].heavyindex > nodes[b].heavyindex) {
ascend(a);
} else {
ascend(b);
}
}
1) 是吗
2) 我想在函数中移动#define
的代码,以更好地理解它的作用,但我如何翻译下面的行呢?
m##i = max(m##i + paths[h].ftree.sum(p), paths[h].stree.max_(0, p));
- 是的
-
正如Ben Voigt在评论中提到的,
##
是标记粘贴操作符。因此,定义了#define f(i) m##i
后,f(a)
将扩展到ma
,f(b)
将扩展到mb
,等等。由于这只能通过预处理器实现,所以必须考虑其他方法将其作为函数来实现。通过引用传递
ma
和mb
将是一个好主意。它可能看起来像这样:ascend(T& mi) { ... mi = max(mi + paths[h].ftree.sum(p), paths[h].stree.max_(0, p)); ... }
其中,
T
是ma
和mb
的类型。如果它们是不同类型的,则需要将其作为函数模板。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 不断"Attempting to reference a deleted function"
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 模板规范获取'Ambiguous call to overloaded function'
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 如何修复"too few arguments to function"?
- std::bind to void* to std::function
- "No suitable conversion function from 'std::string' to 'const char *' exists"
- 结构错误:"no matching function for call to "
- C++线程"Call to non-static member function without an object argument"
- 替换派生对象向量中的对象"no matching function to call"
- add_edge导致"no function to call error"
- reinterpret_cast std::function* to and from void*
- 如何修复C++中的"no matching function to call to..."?
- 调用布尔函数但收到错误"no matching function to call"?
- Function to Lambda
- C++ 错误:使用 argc、*argv[] 和多维数组调用 void 函数时"no matching function to call to "
- 构造函数模板调用'no matching function to call'