Lambda reinitialize vector - why does it work?

Why the next compiler?

vector<int> vec;

auto lambda = [ vec (move(vec)) ]() {  //??      
};

How can I reinitialize an already assigned vec variable with vec (move(vec))? Doesn't that call the move constructor?

If I write:

vector<int> vec;
vec (move(vec));

it is not valid

+4
source share
2 answers

This was a change that did not make it to C ++ 11, but C ++ 14, we can see the rationale for N3610 :

++ 11 lambdas . ++ 11 NB , - JP9 CD1, - FI8 FCD. FI8 Core , , , . , iostreams ( stringstreams) unique_ptrs . Evolution -2012 , " ++ 11". , ;

N3648:

init-capture , init-capture . , . form "auto init-capture;", , (.. init-capture) . [ . init "x = std:: move (x)"; "x" . --end note ] init. lambda-expression lambda-declarator , init-capture , -. [:

int x = 4;
auto y = [&r = x, x = x+1]()->int {
            r += 2;
            return x+2;
         }();  // Updates ::x to 6, and initializes y to 7.

- ]

++ 14 N3936, .

+4

init-capture. , vec. -:

init , "auto init-capture ;" -, [..]

cppreference.

+5

All Articles