2021年3月25日星期四

non-const lvalues and rvalues giving errors

I already found the solution (at least I think I did) to a problem I had, but I want to understand why it works and not work.

I have

std::vector<std::string> a_vector_of_strings;  

and then

for (auto& a_string : a_vector_of_strings){      //do something with a_string here            auto another_string = a_string+"something_more"; //HERE  }  

Originally I wanted to do this in the line marked "HERE"

auto &another_string = a_string+"something_more";   

but this gave the error

cannot bind non-const lvalue reference of type 'std::__cxx11::basic_string<char>&' to an rvalue of type 'std::__cxx11::basic_string<char>'  

Can someone explain me the inner workings of this so that I don't repeat the mistake?

https://stackoverflow.com/questions/66809637/non-const-lvalues-and-rvalues-giving-errors March 26, 2021 at 09:03AM

没有评论:

发表评论