This is a rather general question. In this post Where does a std::vector allocate its memory? it says that vector
allocates its elements in the heap by default. In C++11, there is move semantics and vector
supports this. What if I have an object which has only move constructor, and I have declared this object in the stack, now I want to push_back
this object into a vector
, is the one being pushed back still in the heap?
An example might be pushing back a stack declared std::thread t1(someFunc)
into a vector
, as following
int main(){
std::thread t1(someFunc);
std::vector<std::thread> threads;
threads.push_back(t1); // is the one being pushed back in the heap
// or in the stack? Is the answer generally
// true for objects which have only move
// constructors?
}