move semantic

Download Move semantic

Post on 15-Jul-2015

312 views

Category:

Documents

9 download

Embed Size (px)

TRANSCRIPT

Move Semantic Rvalue Reference

Move SemanticRvalue ReferenceRvalue ReferenceMove SemanticRvalue & LvalueRvalue ReferenceCopy constructor & Move constructorVisual Studio 2010 2012 Rvalue Reference . , .2Move Semantic (?!)

.Move Semantic . . . Rvalue Reference .

.3Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vec .vector push_back , .

.4Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vec 10 reserve .5Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmm 10 .6Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmm push_back vector .7Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmmTTTTT...mmmmm .

8Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmmTTTTT...mmmmmTm .9Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmmTTTTT...mmmmmTm 10Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmmTm . .

Move Semantic .11Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmm 10 . T .12Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmm .13Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mTmmmmm . .14Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmmTTTTT...mT .15Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecTTTTT...mmmmmTTTTT...mT vector 16Move Semantic (vector)vector vec;vec.reserve(10);... // 10 .

vec.push_back( T() );vecmmmmmTTTTT...mT . .17Move Semantictemplatevoid Swap(T& a, T& b) { T t(a); a = b; b = t;}abab Swap . .18Move Semantictemplatevoid Swap(T& a, T& b) { T t(a); a = b; b = t;}copy of aabtab a t . a .19Move Semantictemplatevoid Swap(T& a, T& b) { T t(a); a = b; b = t;}copy of acopy of bbtab a b . b a .20Move Semantictemplatevoid Swap(T& a, T& b) { T t(a); a = b; b = t;}copy of acopy of bcopy of atab t a b .21Move Semantictemplatevoid Swap(T& a, T& b) { T t(a); a = b; b = t;}copy of bcopy of aab t . 3 1 .

Move Semantic .22Move Semantictemplatevoid Swap(T& a, T& b) { T t(std::move(a)); a = std::move(b); b = std::move(t);}abab STL Swap .move . .23Move Semantictemplatevoid Swap(T& a, T& b) { T t(std::move(a)); a = std::move(b); b = std::move(t);}abtab T a . a t .24Move Semantictemplatevoid Swap(T& a, T& b) { T t(std::move(a)); a = std::move(b); b = std::move(t);}abtab b a .25Move Semantictemplatevoid Swap(T& a, T& b) { T t(std::move(a)); a = std::move(b); b = std::move(t);}abtab t b .26Move Semantictemplatevoid Swap(T& a, T& b) { T t(std::move(a)); a = std::move(b); b = std::move(t);}abab t . .T .27 ...

.... .... . . .

Lvalue Rvalue .28Rvalue & LvalueRvalue Lvalue . ?Rvalue Lvalue . ?

Rvalue Reference . ? .29Rvalue & LvalueRvalue Lvalue .Rvalue ...Lvalue ...

Rvalue R Right Lvalue L Left ...30Rvalue & LvalueRvalue Lvalue .Rvalue ...Lvalue ............

.

Lvalue .31Rvalue & LvalueRvalue Lvalue .Rvalue Lvalue

int a = 10;int b = a;

vector getVector();

void func( std::string );func(hello world);Rvalue , Lvalue .Rvalue .

. Rvalue Lvalue .

a a . Lvalue.10 . Rvalue .

vector . ? ? Rvalue . Rvalue .

?hello world Rvalue .std::string char* ? . Rvalue . Rvalue string . string Lvalue .

?32Rvalue & Lvalue

Rvalue Lvalue ? .33Rvalue ReferenceRvalue !

int&& a = 10;a = 20;// const .int b = a;int&& c = b;//error!!! Rvalue Reference . . Rvalue . .

. 10 a . 10 a . . ? a 10 . 4 .34Rvalue ReferenceRvalue !

void func( T& a) { cout