The following program uses the append_list
function with the rvalue reference signature, not the const reference one. Why?
#include <stdio.h>
#include <iterator>
#include <memory>
#include <vector>
class foo {
public:
std::vector<int> bar{1};
};
template<typename T, typename U>
static void append_list(T &t, const U &u) {
t.insert(t.end(), u.begin(), u.end());
}
template<typename T, typename U>
static void append_list(T &t, U &&u) {
printf("move\n");
std::move(u.begin(), u.end(), std::back_inserter(t));
}
int main() {
auto shmoo = std::make_shared<foo>();
std::vector<int> baz{2};
append_list(baz, shmoo->bar);
}
AFAICS shmoo->bar
should be an lvalue reference to the bar field of the shmoo
object. I don't see a "conversion sequence" here to make an rvalue reference out of it, but I admit there's a lot going on here I don't understand.