Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass by reference more expensive than pass by value

Is there a case where pass-by-reference is more expensive than pass-by-value in C++? If so, what would that case be?

like image 746
erelender Avatar asked Jan 21 '10 09:01

erelender


1 Answers

Prefer passing primitive types (int, char, float, ...) and POD structs that are cheap to copy (Point, complex) by value.

This will be more efficient than the indirection required when passing by reference.

See Boost's Call Traits.

The template class call_traits<T> encapsulates the "best" method to pass a parameter of some type T to or from a function, and consists of a collection of typedefs defined as in the table below. The purpose of call_traits is to ensure that problems like "references to references" never occur, and that parameters are passed in the most efficient manner possible.

like image 95
Gregory Pakosz Avatar answered Nov 12 '22 18:11

Gregory Pakosz