help!!!! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

help!!!!

int main(){ string names[4]={"ddd","fff","bbb","xxx"}; //does not work for( string&& i :names){ cout <<i <<endl; } //does not work for(const auto&& i :names){ cout <<i <<endl; } //it works i meant auto&& and string&& are same thing here ,don't they?the damn part is what is wrong with const auto&& for( auto&& i :names){ cout <<i <<endl; } }

3rd Mar 2017, 5:54 PM
Navid Tak
Navid Tak - avatar
4 Answers
+ 4
so...? what is your question?
3rd Mar 2017, 3:16 PM
RJP
RJP - avatar
+ 3
No, string&& and auto&& are not the same thing. string&& is trying to declare a rvalue reference as a lvalue. With auto&& auto will decide the type, this is really the same as const auto& This may help. http://stackoverflow.com/questions/5481539/what-does-t-double-ampersand-mean-in-c11 valid signatures are: for (string i: names) for (string& i: names) and: int main(){ string names[4]={"ddd","fff","bbb","xxx"}; for(const string& i :names){ cout <<i <<endl; } for(const auto& i :names){ cout <<i <<endl; } for( auto&& i :names){ cout <<i <<endl; } return 0; }
3rd Mar 2017, 3:35 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
The question is why auto&& works but string&& ,const auto&& dont?
3rd Mar 2017, 3:23 PM
Navid Tak
Navid Tak - avatar
0
ok thank you guys but the point I get from all your answer is const can change lvalue reference to accept rvalue, the new question is, how this work? a quick description will be very useful
3rd Mar 2017, 6:29 PM
Navid Tak
Navid Tak - avatar