Does a copy still take place here:
std::vector<int> &f = foo();
where foo‘s prototype is
std::vector<int> foo();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
foo()returns a temporary object which cannot be bound to non-const reference.Do one of these:
Read the comments above. In the second version (non-reference version), the compiler may optimize the return value, avoiding the copying of temporary. It depends on the implementation of
foo()as well.See this: