Are moved from string and vector required to not own any heap memory?

Viewed 89

I know moved from objects are in unspecified but destructible state and I know generally that means that they can own memory, file handles... But I do not know if moved from std::strings and std::vectors are allowed to own any memory.

So for example is the following function potentially leaking memory or is it fine according to C++ standard?

void f(){
    std::aligned_storage_t<sizeof(std::string), alignof(std::string)> memory;
    std::string& src = *new (&memory) std::string ("98->03->11->14->17->20");
    std::string dest(std::move(src ));
}

notes:

  • I am interested in ISO standard, I know that for most obvious implementation src should not be owning any memory after move, I am interested in "legal" status of this code.

  • I know code presented here is not "proper" way to code in C++, it is just an example to explain my question

  • I am asking specifically about std::string and std::vector, I know this is not generally true
2 Answers
Related