?????????????????????????????????????????????????????????ū?????????????????Щ????????????
????????????????????????????????????????????????????????????????????
????*************************Code*************************
????class Dog
????{
????public:
????Dog(){}
????virtual ~Dog(){}
????};
????void NonConstReference (Dog & dog )
????{
????//tell the dog to do something here
????}
????void TestNonConstReference ()
????{
????NonConstReference( Dog());
????}
????*************************VS 2013?? Level4 (/W4)*************************
????warning C4239: nonstandard extension used : 'argument' : conversion from 'Dog' to 'Dog &'
????*************************GCC?? C++11*************************
????-------------- Build: Debug in Test (compiler: GNU GCC Compiler)---------------
????mingw32-g++.exe -Wall -fexceptions -g -std=c++11  -c G:MyBackupcodeCodeBlockTestmain.cpp -o objDebugmain.o
????G:MyBackupcodeCodeBlockTestmain.cpp: In function 'void TestNonConstReference()':
????G:MyBackupcodeCodeBlockTestmain.cpp:18:29: error: invalid initialization of non-const reference of type 'Dog&' from an rvalue of type 'Dog'
????G:MyBackupcodeCodeBlockTestmain.cpp:11:6: error: in passing argument 1 of 'void NonConstReference(Dog&)'
????Process terminated with status 1 (0 minute(s)?? 0 second(s))
????2 error(s)?? 0 warning(s) (0 minute(s)?? 0 second(s))
????*************************lvalue?? xvalue?? prvalue???????*************************
????????lvalue?? rvalue ??????????????κ????????????????????1????????
????lvalue????????????????????磺
????E???????*E??lvalue
??????????????????????????????????lvalue??????int& foo();
????xvalue???????????????lvalue????????????????????
????prvalue?????????????????????????????????????з?????κζ??????????磺
??????????????????????????????????rvalue??????int foo();
??????з?????κζ?????????5.3??true??
????*************************lvalue?? xvalue?? prvalue??????*************************
?????????????????C++ PROGRAMMING LANGUAGE 4TH EDTION??
????There are two properties that matter for an object when it comes to addressing?? copying?? and moving:
????? Has identity: The program has the name of?? pointer to?? or reference to the object so that it is possible to determine if two objects are the same?? whether the value of the object has changed?? etc.
????? Movable: The object may be moved from (i.e.?? we are allowed to move its value to another location and leave the object in a valid but unspecified state?? rather than copying;).
????It turns out that three of the four possible combinations of those two properties are needed to precisely describe the C++ language rules (we have no need for objects that do not have identity and
????cannot be moved).
????Using ‘‘m for movable’’ and ‘‘i for has identity??’’ we can represent this classification of expressions graphically:
????So?? a classical lvalue is something that has identity and cannot be moved (because we could examine it after a move)?? and
????a classical rvalue is anything that we are allowed to move from.
????*************************ISO IEC 14882 2011 8.5.3 References*************************
????ISO??????cv??????const volatile ???η???
????????????????????????????????????cv1 T1 dest = cv2 T2 src??
??????????????
????int src = 123;
????const int& dest = src;
????void function(const int& dest){};
????function(src);
????ISO????????????????????reference-related?? reference-compatible??
????Given types “cv1 T1” and “cv2 T2??” “cv1 T1” is reference-related to “cv2 T2” if
????T1 is the same type as T2?? or
????T1 is a base class of T2.
????“cv1 T1” is reference-compatible with “cv2 T2” if
????T1 is reference-related to T2 and
????cv1 is the same cv-qualification as?? or greater cv-qualification than?? cv2.
?????????cv1 >= cv2???????????Щ???const > ??????η??? const volatile > const??etc.
??????????θ????cv1 T1 dest = cv2 T2 src; ?????????????4?????裺
????1.???dest ?????lvalue reference??????
????1.1???src?????????????????bit-filed????????cv1 T1 ?? reference-compatible with cv2 T2???
????1.2???T2????????????class?? struct?? union?? etc.???????cv1 T1 ???? reference-compatible with cv2 T2?????cv2 T2??????????cv3 T3???????????(src1)????????cv1 T1 ?? reference-compatible with cv3 T3???
?????????dest ????src??????src1???
????2.???cv2 T2 src????????1.1??1.2?????cv1 ????????????const??lvalue reference???壬??????????????rvalue reference????????cv2 T2??????????????
????2.1???src?????xvalue?? ???????prvalue?? array prvalue ?????????????????????cv1 T1 ?? reference-compatible with cv2 T2???
????2.2???cv2 T2?????????????cv1 T1 ???? reference-compatible with cv2 T2?????cv2 T2??????????cv3 T3????????2.1?漲???????????src1??
?????????dest????src??????src1???
????3.???cv2 T2 src?????????2.1??2.2????????????src????????????????
????3.1??????????????????????cv1 T1 ?? reference-related with cv2 T2??????cv1 >= cv2??
????4.???cv2 T2 src???????????????е??????????cv1 T1????????rvalue reference??????????cv2 T2?????lvalue??????????????????
????*************************Reference ????????????*************************
????**************************************??????Щ????**************************************
????-------------------------------???????1???????-------------------------------------------------
????double d = 2.0;
????double& rd = d; //d?? is an lvalue?? and the cv1 equals cv2?? 1.1???????
????const double& rcd = d; // d?? is an lvalue??
????// the cv1 >= cv2: const > ??????η???1.1???????
????struct A { };
????struct B : A
????{
????operator int&();
????} b;
????A& ra = b; // b?? has a class type: struct;
????//cv1 is reference related with cv2?? ra is the base class of the b??1.2???????
????const A& rca = b; // b?? has a class type?? struct;
????//cv1 is reference related with cv2?? ra is the base class of the b;
????// the cv1 >= cv2: const > ??????η???1.2???????
????int& ir = B(); // B()?? has a class type: struct
????//it can be converted to an lvalue of the int&: operator int&()
????//cv1 == cv2: cv???η???????1.2???????