From #define to function

From #define to function

本文关键字:function to #define From      更新时间:2023-10-16

我在函数中有这段代码,但我不明白它的作用。

....
#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)); 
  1. 是的
  2. 正如Ben Voigt在评论中提到的,##是标记粘贴操作符。因此,定义了#define f(i) m##i后,f(a)将扩展到maf(b)将扩展到mb,等等。

    由于这只能通过预处理器实现,所以必须考虑其他方法将其作为函数来实现。通过引用传递mamb将是一个好主意。它可能看起来像这样:

    ascend(T& mi) {
        ...
        mi = max(mi + paths[h].ftree.sum(p), paths[h].stree.max_(0, p)); 
        ...
    }
    

    其中,Tmamb的类型。如果它们是不同类型的,则需要将其作为函数模板。