Tôi đang cố gắng hiểu tại sao ai đó lại viết một hàm có tham chiếu const rvalue .
Trong ví dụ mã bên dưới mục đích là hàm tham chiếu const rvalue (trả về "3"). Và tại sao độ phân giải quá tải lại ưu tiên giá trị const ở trên hàm tham chiếu const LValue (trả về "2").
#include <string>
#include <vector>
#include <iostream>
std::vector<std::string> createVector() { return std::vector<std::string>(); }
//takes movable rvalue
void func(std::vector<std::string> &&p) { std::cout << "1"; }
//takes const lvalue
void func(const std::vector<std::string> &p) { std::cout << "2"; }
//takes const rvalue???
//what is the point of const rvalue? if const I assume it is not movable?
void func(const std::vector<std::string> &&p) { std::cout << "3"; }
int main()
{
func(createVector());
return 0;
}