How does std::forward receive the correct argument?

It does bind to the overload of std::forward taking an lvalue: template <class T> constexpr T&& forward(remove_reference_t<T>& t) noexcept; It binds with T == int. This function is specified to return: static_cast<T&&>(t) Because the T in f deduced to int. So this overload casts the lvalue int to xvalue with: static_cast<int&&>(t) Thus calling the g(int&&) … Read more

problems with Move constructor and Move overloaded assignment operator?

Your class A has several issues: Your assignment operator don’t handle self assignment and leak: A& A::operator=(const A& a) { std::cout<<“overload operator=\n”; if (this != &a) { p = a.p; delete[] s; s = new char[strlen(a.s) + 1]; strcpy(s, a.s); } return *this; } Your move doesn’t move but copy: A::A(A&& a) : p(a.p), s(a.s) … Read more

C++11 rvalue reference calling copy constructor too

Put noexcept on your move constructor: TestClass(TestClass&& other) noexcept { Elaboration: I was going to give this one Pierre, but unfortunately the cppreference source is only approximately correct. In C++03 vector<T>::push_back(T) has the “strong exception guarantee”. That means that if the push_back throws an exception, the vector is left in the same state it had … Read more