Moving (std::move) out class members correctly according to the C++ standard

Viewed 40

If i have a class containing a member which i want to move out like a unique_ptr or unique_lock ... is is correct according to the standard just to move it out just like this:

#include<memory>
class C 
{
    public:
    C(): _data(std::make_unique<int>(42)){ }
    C(const C&) = delete;
    C(C&&) = delete;
    C& operator=(const C&) = delete;
    C& operator=(C&&) = delete;

    [[nodiscard]] std::unique_ptr<int> give_data() {
        return std::move(_data);  // correct?
    }

    private:
    std::unique_ptr<int> _data;
};

int main()
{
    C obj;
    auto data = obj.give_data();
    return *data;
}

according to STL all STL class objects are in a valid but undefined state after move; meaning the class invariant holds. Is this true in general? How can this be improved?

0 Answers
Related