You have many choices to pass function parameters. You can pass by value or by reference. A reference can be const or non-const. You can even move or forward your parameters. Your decision should depend on if it is an in, an out, an in-out, a consume, or a forward function parameter. Curious? Read the post!

According to the guidelines, let's talk about in, out, in-out, consume, or forward parameter.

This is the first advanced rule to consume parameters. Use a rvalue reference if you consume the parameter and move it inside the function body. Here is an example:

voidsink(vector<int>&& v) { // sink takes ownership of whatever the argument owned// usually there might be const accesses of v here
store_somewhere(std::move(v));
// usually no more use of v here; it is moved-from
}

There is an exception to this rule. std::unique_ptr is a move-only type that is cheap to move, therefore, you can move it.

This is the idiom that factory methods such as std::make_unique or std::make_shared use. Both functions take a type T and arbitrary numbers of arguments args and forward them unchanged to the constructor of T. Have a look here:

An explicit return value documents the intention of a function. Using a parameter with a reference as out output value may be misleading. This can also be an in-out value. Returning the result of a function by value holds also for the standard container that uses move semantic implicitly.

What's next

This post was about in, out, in-out, consume, and forward parameters but there are more questions to answer. How should you deal with sequences or with ownership? I will write about it in the next post.

Get your e-book at leanpub:

The C++ Standard Library

Concurrency With Modern C++

Get Both as one Bundle

With C++11 and C++14 we got a lot of new C++ libraries. In addition, the existing ones are greatly improved. The key idea of my book is to give you the necessary information to the current C++ libraries in about 200 pages.

C++11 is the first C++ standard that deals with concurrency. The story goes on with C++17 and will continue with C++20.

I'll give you a detailed insight in the current and the upcoming concurrency in C++. This insight includes the theory and a lot of practice with more the 100 source files.

Get my books "The C++ Standard Library" and "Concurrency with Modern C++" in a bundle.

In sum, you get more than 500 pages full of modern C++ and more than 100 source files presenting concurrency in practice.