page-fault

Tweaks

Consider pass by value for copyable parameters that are cheap to move and always copied.

Sometimes we want to ‘sink’ a function argument when we call a function:

class Widget {
public:
    // Both of these functions are going to make a copy of the in parameter

    // This function makes a copy via the copy constructor
    void add (const std::string& in) { strings.push_back (in); }

    // This function makes a copy via the move constructor
    void add (std::string&& in) { strings.push_back (std::move (in)); }
private:
    std::vector<std::string> strings;
};

Writing two copies of a function which both do essentially the same thing is awkward to maintain and increases the size of the resulting binary.

We could write a single function taking a forwarding reference instead, but that has drawbacks like

Another option is to write a single function which accepts its argument by value:

void add (std::string in) { strings.push_back (std::move (in)); }

This version allows us to hide the function implementation, and ensures we only have a single version of the function in the resulting binary. Also, lvalues are copied and rvalues are moved, which is the behaviour we want. However, this approach has some performance implications.

When is passing by value acceptable?

Guidelines

Consider emplacement instead of insertion.

Emplacement functions like emplace_back allow us to avoid the creation and destruction of temporaries when constructing objects into containers. They should always be at least as efficient as their push variants, and sometimes will be more efficient. Unfortunately, in reality this isn’t always the case, so profiling will be required if performance is a concern. However, in the following cases, emplacement is all but guaranteed to be faster:

Emplacement has a couple of gotchas:

Guidelines