简体   繁体   English

宏扩展与模板

[英]macro expansion with templates

I'm writing several functions which takes as input the result of a template function: 我正在编写几个函数,它们将模板函数的结果作为输入:

int alg1(Vect3) {...}
...
int algN(Vect3) {...}

void main() {
    alg1( mat.topRightCorner<3,1>() )
}

where, if you are curious, topRightCorner returns a submatrix of mat , a method from the Eigen , where the dimension are placed as template parameters when are known at compile time. 其中,如果你很好奇, topRightCorner返回mat的子矩阵,这是一个来自Eigen的方法,其中维度在编译时已知为模板参数。

However creating a "shortcut" using a macro to quickly switch between different algorithms (since in the real code the function is called many times), like this 然而创建一个“快捷方式”使用宏来快速切换不同的算法(因为在实际代码中,函数被多次调用),像这样

#define ALG(X)    ( algN(X) )

ALG( mat.topRightCorner<3,1>() )

gives an error, since the macro is correctly expanded but is somehow misinterpreted as with two different parameters, mat.topRightCorner<3 and 1>() . 给出一个错误,因为宏被正确扩展但在某种程度上误解为有两个不同的参数, mat.topRightCorner<31>()

Wrapping the input with brackets will do the trick, but why this behaviour? 使用括号包装输入将起到作用,但为什么会出现这种情况?

Because , is accepted by the preprocessor as a delimiter for a new macro argument, and because the preprocessor doesn't really care that you might have instead meant it as a delimiter for a template parameter list. 因为,由预处理接受为新的宏参数的分隔符,因为预处理器并不真正关心,你可能有,而不是意味着它作为一个模板参数列表的分隔符。

To be slightly more precise: 更准确一点:

ALG( mat.topRightCorner<3,1>() )
     ^^^^^^^^^^^^^^^^^^^^ ^^^^

Both of these lexically look like valid macro arguments, and macro parsing takes priority. 这两个词汇看起来都像有效的宏参数,宏解析优先。

On the other hand, the preprocessor is aware of what () does, so you can "force" parsing as a single argument that way. 另一方面,预处理器知道what ()作用,因此你可以通过这种方式“强制”解析为单个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM