I have a class that mimicks a std::vector. For this, I want to have a function push_back that accepts both an lvalue and a rvalue.
void push_back(const T& obj) {
push_back(std::move(obj));
}
void push_back(T&& obj) {
buffer[numElements] = obj;
numElements = numElements+1>=Sz-1 ? Sz-1 : numElements+1;
}
However, this code ends in an endless recursion when I pass an lvalue due to move, it calls the wrong function (I would expect const T& obj
function to call T&& obj
overload).
The doc of std::move says
It is exactly equivalent to a static_cast to an rvalue reference type.
What am I missing?
Issue is that, in
void push_back(const T& obj) {
push_back(std::move(obj));
}
you got const T&&
with std::move
.
So only viable candidate is void push_back(const T&)
, so infinite recursive call.