Search code examples
c++inline

should I take arguments to inline functions by reference or value?


Is one of these faster?

inline int ProcessByValue(int i)
{
    // process i somehow
}

inline int ProcessByReference(const int& i)
{
    // process i somehow
}

I know that integral types should be passed by value. However, I am concerned that the compiler might inline ProcessByValue to contain a copy. Is there a rule for this?


Solution

  • The parameter should be typed according to what makes sense for the function.

    If the function takes a primitive type, pass by value would make sense. Some people I know would complain if it were passed by const ref (as it's 'unnecessary'), but I don't think I'd complain. If the function takes a user defined type and doesn't modify the parameter, then pass by const ref would make sense.

    If it's a user defined type and the parameter is modified, then the semantics of the function would dictate how it should be passed.