Pass by Value When Applicable
Learn about the usage of pass-by value in different cases.
We'll cover the following...
We'll cover the following...
Passing by value: when and why to use it
Consider a function that converts a std::string to lowercase. In order to use the move-constructor where applicable, and the copy-constructor otherwise, it may seem like two functions are required:
However, by taking the std::string by value instead, we can write one function that covers both cases:
Let's see why this implementation of str_to_lower() avoids unnecessary copying where possible. When passed a regular variable, shown as follows, the content of str is copy-constructed into ...