Search code examples
c++performancec++11const-reference

Inline Function Arguments Passing


Is there a need performance-wise for inline functions to pass its arguments by const reference like

foo(const T & a, const T &b)

compared to by value

foo(T a, T b)

if I don't change the values of a and b in the function? Does C++11 change recommend anything specific here?


Solution

  • Pass by value can only elide the copy-constructor call if the argument is a temporary.

    Passing primitive types by const reference will have no cost when the function is inlined. But passing a complex lvalue by value will impose a potentially expensive copy constructor call. So prefer to pass by const reference (if aliasing is not a problem).