Moving semantics and structures

I have a function

struct foo {
    std::vector<int> v;
};

foo func();

will the vector inside foo move or copy when it returns from the function?

+5
source share
1 answer

He will be moved . (*)

Since you do not provide an explicit move constructor for your class foo, the compiler will implicitly generate one for you that calls the move constructor (if available) for all members of your class. Because the std::vectormove constructor defines it, it will be called.

In paragraph 12.8 / 15 of the C ++ 11 standard:

An implicit copy / move constructor for a non-unit class X performs a phased copy / move of its bases and members. [...]

, copy/move . () .

(*) , - foo() .

+6

All Articles