Below code snippet triggers a compilation error error: cannot bind rvalue reference of type ‘std::__cxx11::basic_string&&’ to lvalue of type ‘std::__cxx11::basic_string’
.
#include <iostream>
std::string&& foo(std::string&& str)
{
return std::move(str);
}
decltype(auto) bar(std::string&& str)
{
decltype(auto) r{foo(std::move(str))};
return r;
}
int main()
{
std::string str{"Hello, World!"};
auto&& ret{bar(std::move(str))};
return 0;
}
According to decltype(auto)
rules the local variable r
has std::string&&
type. Where is an lvalue involved in the return sequence that is caught by the compiler?