ereAin'tNoSuchingAsAUniversalReference
ereAin'tNoSuchingAsAUniversalReference
JonathanWakely
RedHat
1of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
UniversalReferences
ScoMeyers'sterm(Overload111,blog,Channel9video)T&&Doesn'tAlwaysMean"RvalueReference"
2of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
UniversalReferences
Scopresentsaveryusefulmodelforunderstandingfunctiontemplatessuchas:
templatevoidfunc(T&&arg){}
Whichcanbepassedlvalueorrvaluearguments:
inti=0;
func(i
);
func(i+1);
func("cat"s);
//lvalue//rvalue//rvalue
3of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
UniversalReferences
templatevoidfunc(T&&){}
"eessenceoftheissueisthat"&&"inatypedeclarationsometimesmeansrvaluereference,butsometimesitmeanseitherrvaluereferenceorlvaluereference."
UnlessstatedotherwiseallquotesintheseslidesarefromMeyers,2012,'UniversalReferencesinC++11',Overload,no.111,p.8-12.
4of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
UniversalReferences
Scointroducesnewterminologyfortheservalue-or-lvaluereferences:
"Suchunusuallyflexiblereferencesdeservetheirownname.Icallthemuniversalreferences.""IfavariableorparameterisdeclaredtohavetypeT&&forsomededucedtypeT,thatvariableorparameterisauniversalreference."
(isincludestypesdeducedwithauto&&becauseautousesthesamerulesastemplateargumentdeduction)
5of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
Wedon'tneednostinkin'abstractions!
UniversalreferencesareausefulmodelforreadingandunderstandingtemplatecodeinmodernC++,everyoneshouldreadthearticleButordingtotheC++standard(andpiler)…ereain'tnosuchthingasauniversalreference!
estandard(pilers)onlyknowaboutlvaluereferencesandrvaluereferencesAndSco'srequirementthattheremustbeadeducedtypeinvolvedmeanstherearecasesthataren'texplainedbyuniversalreferences 6of20 ACCU2014 ereAin'tNoSuchingAsAUniversalReference Notauniversalreference templateclassWrapper{public:Wrapper(T&&t)//notdeduced:wrapped(std::forward(t)){}//...private:Twrapped;};
templateWrapperwrap(T&&t)//deduced{returnWrapper{std::forward(t)};}
7of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
Behindthecurtain
erearetwopropertiesoftheC++languagethatexplaineverythingcoveredbythetermuniversalreferences,andothercasestoo:ReferencecollapsingAspecialcaseintheargumentdeductionrules
8of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
ReferenceCollapsing
Let'sdeclareasimpletypedefforareferencetype:
typedefint&
intlref;
Andthenapplysometransformations:
typedefintlref&intlref2;typedefintlref&&intlref3;
Westillhavethesametype:
is_same//true,int&is_same//true,int&
9of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
ReferenceCollapsing
Usingatypedefforanrvalue-referencetype:
typedefint&&
intrref;
enapplyingthesametransformations:
typedefintrref&intlref4;typedefintrref&&intrref2;
Wedon'talwaysgetthesametype:
is_same//false,int&is_same//true,int&&
10of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
ReferenceCollapsing
WecanuseC++11aliastemplatestoperformthesesametransformations:templateusingref=T&;templateusingrefref=T&&;is_same,int&>is_same,int&>is_same,int&>is_same,int&&>is_same,int&>is_same,int&&>
11of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
ReferenceCollapsing
esecorelanguagerulesforreferencecollapsingaremodelledbytransformationtraitsinthestandardlibrary:
add_lvalue_reference::typeadd_lvalue_reference::typeadd_lvalue_reference::type
⇒int&⇒int&⇒int&
add_rvalue_reference::typeadd_rvalue_reference::typeadd_rvalue_reference::type
⇒int&&⇒int&⇒int&&
ispropertyofC++iswhystd::moveisdeclaredtoreturnremove_reference::type&&andnotsimplyT&&,youcan'tjustappend&&toatypeandexpecttogetanrvalue-reference
12of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
ReferenceCollapsing
eexactsameruleswe'veseenforreferencecollapsingintypedefsalsoapplyinothercontexts,suchasfunctionparameters:voidfunc(intlref&);voidfunc(ref);voidfunc(intlref&&);voidfunc(refref);
Allthosedeclarationsarethesamefunctionas:voidfunc(int&);
13of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
ExplicitTemplateArguments
Usingthisfunctiontemplate:templatevoidfunc(T&&){}
WecansubstitutethetypeTusinganexplicittemplateargument:func
(1);//func(int&&)inti;func(i);//func(int&)func
(1);//func(int&&)
14of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
Notauniversalreference
templateclassWrapper{public:Wrapper(T&&t)//notdeduced,butcollapses:wrapped(std::forward(t)){}//...private:Twrapped;};
templateWrapperwrap(T&&t)//deduced{returnWrapper{std::forward(t)};}
15of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
TemplateArgumentDeduction
Usingthesamefunctiontemplate:
templatevoidfunc(T&&){}
WearenowreadytounderstandwhathappenswhenthetypeTisdeduced:
func
(1);//func(int&&)
inti;func(i);
//func(int&)
16of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
Deductionoflvalue-refs
templatevoidfunc(T&&){}
inti;func(i);
//func(int&)
ekeythingtonoticehere,whichisnotatallobvious,isthat
AfunctiontemplatewithaparameterlikeT&&allowsthetemplateparameterTtobededucedasanlvaluereferencetype.
17of20
ACCU2014
ereAin'tNoSuchingAsAUniversalReference
Deductionoflvalue-refs
isall-importantruleaboutdeducinglvaluereferencetypesishardtofindinthestandardandquiteunderstated:"[…]IfPisanrvaluereferencetoacv-unqualifiedtemplateparameterandtheargumentisanlvalue,thetype"lvaluereferencetoA"isusedinplaceofAfortypededuction.[…]"
ISO/IEC14882:2011,ProgrammingLanguages—C++,[temp.deduct.call]§14.8.2.1¶
3 18of20 ACCU2014 ereAin'tNoSuchingAsAUniversalReference UniversalReferences Firstthereisamountain-thenthereisnomountain-thenthereis—Zenproverb Althoughit'svaluabletounderstandwhatyourcodeisactuallydoing,universalreferenceareausefulmodelforreadingandunderstandingC++11code,eveniftheyaren'treallythere!
19of20 ACCU2014 ereAin'tNoSuchingAsAUniversalReference eseslidesareavailableathps:///u/u2014 YoucandownloadandviewtheHTMLversionoftheseslidesmandslike:gitcloneu2014.gitfirefoxu2014/tanstaaur.html 20of20 ACCU2014
UniversalreferencesareausefulmodelforreadingandunderstandingtemplatecodeinmodernC++,everyoneshouldreadthearticleButordingtotheC++standard(andpiler)…ereain'tnosuchthingasauniversalreference!
estandard(pilers)onlyknowaboutlvaluereferencesandrvaluereferencesAndSco'srequirementthattheremustbeadeducedtypeinvolvedmeanstherearecasesthataren'texplainedbyuniversalreferences 6of20 ACCU2014 ereAin'tNoSuchingAsAUniversalReference Notauniversalreference template
(1);//func
(1);//func
(1);//func
3 18of20 ACCU2014 ereAin'tNoSuchingAsAUniversalReference UniversalReferences Firstthereisamountain-thenthereisnomountain-thenthereis—Zenproverb Althoughit'svaluabletounderstandwhatyourcodeisactuallydoing,universalreferenceareausefulmodelforreadingandunderstandingC++11code,eveniftheyaren'treallythere!
19of20 ACCU2014 ereAin'tNoSuchingAsAUniversalReference eseslidesareavailableathps:///u/u2014 YoucandownloadandviewtheHTMLversionoftheseslidesmandslike:gitcloneu2014.gitfirefoxu2014/tanstaaur.html 20of20 ACCU2014
声明:
该资讯来自于互联网网友发布,如有侵犯您的权益请联系我们。