std::vector<std::string> func(int num) {
std::vector<std::string> vec;
int sum = 0;
for(int i = 0; i < num; i++) {
sum +=i;
std::string s = std::to_string(sum);
vec.emplace_back(s);
}
return vec;
}
what's the difference between the code below about using rvalue and lvalue reference.
std::vector<std::string> res = func(10); // (case 1)
std::vector<std::string> &&res = func(10); // (case 2)
std::vector<std::string> &res = func(10); // I got an error!, case3
const std::vector<std::string> &res = func(10); // case4
Question:
- can the rvalue reference(case 2) save a memory copy? than case1
- why lvalue reference(case3) got an error but it work with the
const
(case4)?
Aucun commentaire:
Enregistrer un commentaire