copy vs std::move for ints

In this example, there is no difference. We will end up with 3 ints with value 100. There could definitely be a difference with different types though. For instance, let’s consider something like vector<int>: std::vector<int> a = {1, 2, 3, 4, 5}; // a has size 5 auto a_copy = a; // copy a. now … Read more

Is a `=default` move constructor equivalent to a member-wise move constructor?

Yes both are the same. But struct Example { string a, b; Example(Example&& mE) = default; Example& operator=(Example&& mE) = default; } This version will permits you to skip the body definition. However, you have to follow some rules when you declare explicitly-defaulted-functions : 8.4.2 Explicitly-defaulted functions [dcl.fct.def.default] A function definition of the form: attribute-speciļ¬er-seqopt … Read more

Can I typically/always use std::forward instead of std::move?

The two are very different and complementary tools. std::move deduces the argument and unconditionally creates an rvalue expression. This makes sense to apply to an actual object or variable. std::forward takes a mandatory template argument (you must specify this!) and magically creates an lvalue or an rvalue expression depending on what the type was (by … Read more

Initializer-list-constructing a vector of noncopyable (but movable) objects

Maybe this clause from 8.5.4.5 explains it (my emphasis): An object of type std::initializer_list is constructed from an initializer list as if the implementation allocated an array of N elements of type E, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element … Read more