C++ Pass by Reference vs Pass by Value – Cost Comparison

c++pass-by-referencepass-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?

Best Answer

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.

Related Question