2

我有一个inline函数,它根据输入参数进行一些初始化,并且想知道我是否应该const为参数使用关键字,这会使编译器进行更多优化吗?例如这个伪代码:

inline void init(ENUM1 e1, ENUM2 e2, bool b1, bool b2, ENUM3 e3)
{
    if (b1) { … }

    switch (e2) {
      …
    }

    // And so on…
}

编辑:

关于同一件事的另一个问题。当这个函数init()被调用时,调用会被这个函数内的整个代码替换,还是只替换为适合参数的部分。例如,如果b1为真,调用是否会被替换为if (b1) { … }或仅被替换为 if 括号中的代码。和同样的switch

这就是我的意思,如果添加const会有所帮助。

4

1 回答 1

1

const, used in inline code or not, is unlikely to preclude optimizations, but it may not necessarily allow for more. But if it applies, there's no good reason not to tell the compiler so that it can make the best use of that information that it can.

Update: Since even inline code is generated at compile time, it cannot be tailored to the value of any parameter because it isn't known yet.

于 2014-11-29T13:15:19.820 回答