Situation
I'm designing a class template logic that supports move-semantics. logic has a template parameter Visitor and a reference member that's type is Visitor&. That is a library code.
Users inherits the class template logic and pass a custom visitor such as my_visitor. The custom visitor may contains movable members. For example, my_visitor has a member v that's type is std::vector.
Problem
See test2(). When I move my_logic, my_visitor::v is moved as expected. However, logic<Visitor>::vis refers to the moved from object. Is there any good way to refer to the moved to object?
#include <iostream> #include <vector> // Library code template <typename Visitor> // Concept: Visitor should have visit() struct logic { logic(Visitor& v):vis(v) {} void execute() { vis.visit(); } // Other APIs Visitor& vis; // Other member variables... }; // User code struct my_visitor { my_visitor() { v.push_back(42); } void visit() { std::cout << "expected 1, actual " << v.size() << std::endl; } std::vector<int> v; }; // User inherits all logic's APIs struct my_logic : logic<my_visitor> { my_logic():logic<my_visitor>(mv) {} my_visitor mv; }; void test1() { std::cout << "test1" << std::endl; my_logic m; m.execute(); } void test2() { std::cout << "test2" << std::endl; my_logic m1; { my_logic m2(std::move(m1)); // logic::vis refers to moved from my_visitor... m2.execute(); } } int main() { test1(); test2(); }
my_logic(and probably delete move assignment).