c++ - Pass by reference, constant reference, rvalue-reference, or constant rvalue-reference? -
i learning passing reference, , here test did:
#include <iostream> using namespace std; int = 0; //if uncommented, compiler gives ambiguous definition error. //void paramcheck (string s) { // cout << ++i << ". param var.\n"; //} void paramcheck (const string& s) { cout << ++i << ". param const ref.\n"; } void paramcheck (string& s) { cout << ++i << ". param non-const ref.\n"; } void paramcheck (const string&& s) { cout << ++i << ". param const rvalue-reference.\n"; } void paramcheck (string&& s) { cout << ++i << ". param non-const rvalue-reference.\n"; } int main(int argc, char **argv) { //function call test paramcheck(""); paramcheck(string{""}); string s3{""}; paramcheck(s3); const string s4{""}; paramcheck(s4); //illegal //string& s{""}; //paramcheck(s); const string& s5{s3}; paramcheck(s5); string&& s6{""}; paramcheck(s6); //illegal //const string&& s{s1}; //onstfp(s); //reference test string = s3; = "a changed s3"; cout << s3; { string& b = s3; b = "b changed after assigning s3\n"; cout << "s3 " <<s3; b = s4; b = "b changed after assigning s4\n"; cout << "s3 " <<s3; cout << "s4 " <<s4; } cin.get(); return 0; }
and here result get:
1. param non-const rvalue-reference. 2. param non-const rvalue-reference. 3. param non-const ref. 4. param const ref. 5. param const ref. 6. param non-const ref. s3 b changed after assigning s3 s3 b changed after assigning s4 s4
my question is:
if pass constant expression, triggers non-constant rvalue-reference? under condition trigger constant rvalue-reference (and why s6 not trigging it?)
why non-constant reference , constant rvalue-reference illegal?
i expected cannot change s3, why b in inner scope can change s3? if assigning new object s3 b assigning new reference, why when assign s4 , s3 got changed , s4 empty afterwards?
sorry asking many questions... increase points when questions answered :) reference brings confusion pointer whole new level.
i don't know how increase point... wait 2 days till eligible bounty choose answer.
first code
paramcheck(""); //constructs temporary. temporaries bind `string&&` paramcheck(string{""}); //constructs temporary. temporaries bind `string&&` string s3{""}; paramcheck(s3); //passes reference existing string: `string&` const string s4{""}; paramcheck(s4); //passes reference existing string+const: `const string&` //illegal //string& s{""}; //cannot assign temporary non-const l-reference //what s refer when temporary "dies"? //`const string&` have worked though //paramcheck(s); //passes reference existing string+const: `const string&` const string& s5{s3}; //s5 s3, `const`. paramcheck(s5); //passes reference existing string+const: `const string&` string&& s6{""}; //r-references extend life of temporaries. paramcheck(s6); //passes reference existing strong: `string&` //const string&& s{s1}; //temporaries can extended `t&&` or `const t&` only. //reference test string = s3; //a _copy_ of s3 = "a changed s3"; //so changing copy doesn't effect origional. cout << s3; //s3 still blank, hasn't changed. { string& b = s3; //b isn't "reference" `s3`". `b` _is_ `s3`. b = "b changed after assigning s3\n"; //since `b` `s3`, changes `s3`. cout << "s3 " <<s3; b = s4; //`b` _is_ `s3`, changed `s3` again. b = "b changed after assigning s4\n"; cout << "s3 " <<s3; cout << "s4 " <<s4; //s4 still blank, hasn't changed. }
then questions:
if pass constant expression, triggers non-constant rvalue-reference? under condition trigger constant rvalue-reference (and why s6 not trigging it?)
existing objects pass string&
or const string&
depending on if they're const or not. can copied in string
. temporaries pass string&&
, can copied in string
. there are ways trigger const string&&
, there's no reason ever, doesn't matter. they're shown here.
why non-constant reference , constant rvalue-reference illegal?
the standard says const string&
, string&&
extend lives of temporaries, though i'm not why didn't mention string&
, const string&&
.
i expected cannot change s3, why b in inner scope can change s3? if assigning new object s3 b assigning new reference, why when assign s4 , s3 got changed , s4 empty afterwards?
you initialized b
reference s3
. not copy, reference. means b
refers s3
forever, no matter what. when typed b = "b changed after assigning s3\n";
, that's same s3 = "b changed after assigning s3\n";
. when typed b = s4;
, that's same s3 = s4
. that's reference is. cannot "reseated".
Comments
Post a Comment