Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 216
Nordlöw Avatar asked Oct 02 '11 13:10

Nordlöw


1 Answers

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).

like image 95
Ben Voigt Avatar answered Nov 09 '22 23:11

Ben Voigt