-1

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
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Harsh Raj
  • 3
  • 2
  • Welcome to Stack Overflow. Please read [ask] and **ask a question**. It is not enough to show your code and an error message (although you should also make sure you show a [complete](https://meta.stackoverflow.com/questions/359146) error message, formatted like code). Explain clearly why you need help to solve the problem, and ask for it, starting with a question word like "how" or "why" and ending with a question mark (`?`). – Karl Knechtel Feb 18 '22 at 06:29
  • [Not reproducible](https://godbolt.org/z/4Ez8e9avM) – justANewb stands with Ukraine Feb 18 '22 at 06:36
  • Hi, I got it where i was mistaken, this code works fine with c++11 – Harsh Raj Feb 18 '22 at 06:46

1 Answers1

0

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).