const parameter with inline functions

1.1k views Asked by At

I have an inline function, that does some initialization based on the input parameters and was wondering if I should use the const keyword for the parameters, would that enable the compiler to do some more optimizations? For example this pseudo code:

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

    switch (e2) {
      …
    }

    // And so on…
}

EDIT:

Another question regarding the same thing. When this function init() will get called, will the call get replaced with the whole code inside this function, or only with the parts that fit the parameters. For example, if b1 is true, will the call get replaced with if (b1) { … } or only with the code in the if brackets. And same for the switch?

This is where I meant if adding const will help.

1

There are 1 answers

2
Scott Hunter On BEST ANSWER

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.