I have three questions:
A::str's memory allocated in the scope of function f. After moving it to the element in global var vec, is the block of memory still safe when out the scope of f?
For struct B, not giving a move constructor explicitly, is there a default one like struct A?
struct A { A(const char* p):str(p){} A(const A&& a) : str(std::move(a.str)) { } string str; }; struct B { B(const char* p):str(p){} string str; }; vector<A>vec; void f() { vec.emplace_back(A("hello")); //in vc2010 it will invoke emplace_back(T&&) } int _tmain(int argc, _TCHAR* argv[]) { f(); const char* p = vec[0].str.c_str(); cout << p << endl; return 0; } 3.And can I confirm this dangerous situation never happen in STL container?
struct String { char* pStr; //allocate on heap int* someptr; //if point to allocate on stack size_t len; String (const String&& s) { // something like this: pStr = s.pStr; //ok,safe len = s.len; s.pStr = nullptr; someptr = s.someptr; //danger } };