Consider:
struct Foo {
std::vector<int> data () const { return vec; }
const std::vector<int>& c_data () const { return vec; }
std::vector<int> vec {};
};
auto lambda1 = [] (const std::vector<int>&) {};
auto lambda2 = [] (std::vector<int>&&) {};
auto lambda3 = [] (const auto&) {};
auto lambda4 = [] (auto&& p) {};
And usage is:
Foo f {};
lambda1 (f.data ());
lambda1 (f.c_data ());
lambda2 (f.data ());
lambda2 (f.c_data ()); // (X)
lambda3 (f.data ());
lambda3 (f.c_data ());
lambda4 (f.data ());
lambda4 (f.c_data ()); // (Y)
This code fails to compile because of (X), which is of course understandable by me. We can not bind const reference, to rvalue reference. Fine.
Could someone explain me what is the actual type of lambda4
's p
parameter? Here (Y), compiler does compile it even though I pass const reference argument to it.
What is the difference between lambda2
and lambda4
types in the sense of type deduction?