I am a newbie with this topic in cpp and trying to usnderstand through this code, so please tell me how l-values and r-values make difference in this code and how they are implemented. Also why am i getting error here (do execution of this code depends on c++ version)?
void printName(const std::string& name){
std::cout << "[l-value] " << name << std::endl;
}
void printName(std::string&& name){ //Line 8
std::cout << "[r-value] " << name << std::endl; //Line 9
}
int main(){
std::string firstname = "Harry";
std::string lastname = "Singh";
std::string fullname = firstname + lastname;
printName(firstname);
printName(lastname);
printName(firstname+lastname);
}
I am getting following mentioned error on the following code.
8 error: expected ',' or '...' before '&&' token
9 error: 'name' was not declared in this scope
rvalues references ( &&
) is a C++11 feature and onwards. It's because you're using C++98 or older:
Add this compiler flag -std=c++11
(or -std=c++14
, -std=c++17
, -std=c++20
).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.