std::unique_ptr gives one owner responsibility for an object.

unique ownership A `std::unique_ptr` deletes its object automatically when the owner goes out of scope.

Unique Ptr

text
unique_ptr.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <memory>
#include <string>

class Note {
private:
    std::string text;

public:
    Note(std::string value) {
        text = value;
    }

    std::string read() {
        return text;
    }
};

int main() {
    std::string text = "alpha";

    std::unique_ptr<Note> note = std::make_unique<Note>(text);

    std::cout << "note=" << note->read() << std::endl;
    std::cout << "owned=" << (note != nullptr) << std::endl;
    return 0;
}
#include <iostream>
#include <memory>
#include <string>

class Note {
private:
    std::string text;

public:
    Note(std::string value) {
        text = value;
    }

    std::string read() {
        return text;
    }
};

int main() {
    std::string text = "beta";

    std::unique_ptr<Note> note = std::make_unique<Note>(text);

    std::cout << "note=" << note->read() << std::endl;
    std::cout << "owned=" << (note != nullptr) << std::endl;
    return 0;
}
#include <iostream>
#include <memory>
#include <string>

class Note {
private:
    std::string text;

public:
    Note(std::string value) {
        text = value;
    }

    std::string read() {
        return text;
    }
};

int main() {
    std::string text = "gamma";

    std::unique_ptr<Note> note = std::make_unique<Note>(text);

    std::cout << "note=" << note->read() << std::endl;
    std::cout << "owned=" << (note != nullptr) << std::endl;
    return 0;
}
  1. text ← alpha

    19int main() {20    std::string text→ alpha = "alpha"; //@text="beta", "gamma"2122    std::unique_ptr<Note> note = std::make_unique<Note>(textalpha);
  2. Note(std::string value)

    9public:10    Note(std::string valuealpha) {11        text = valuealpha;12    }
  3. note ← (empty)

    22std::unique_ptr<Note> note→ (empty) = std::make_unique<Note>(textalpha);2324std::cout << "note=" << note(empty)->read() << std::endl;25std::cout << "owned=" << (note != nullptr) << std::endl;
  4. std::string read()

    14std::string read() {15    return textalpha;16}
  5. std::cout << "note=" << note->read() << std::endl;

    24    std::cout << "note=" << note(empty)->read() << std::endl;25    std::cout << "owned=" << (note(empty) != nullptr) << std::endl;26    return 0;27}
    outputnote=alpha
    owned=1
  1. text ← beta

    19int main() {20    std::string text→ beta = "beta";2122    std::unique_ptr<Note> note = std::make_unique<Note>(textbeta);
  2. Note(std::string value)

    9public:10    Note(std::string valuebeta) {11        text = valuebeta;12    }
  3. note ← (empty)

    22std::unique_ptr<Note> note→ (empty) = std::make_unique<Note>(textbeta);2324std::cout << "note=" << note(empty)->read() << std::endl;25std::cout << "owned=" << (note != nullptr) << std::endl;
  4. std::string read()

    14std::string read() {15    return textbeta;16}
  5. std::cout << "note=" << note->read() << std::endl;

    24    std::cout << "note=" << note(empty)->read() << std::endl;25    std::cout << "owned=" << (note(empty) != nullptr) << std::endl;26    return 0;27}
    outputnote=beta
    owned=1
  1. text ← gamma

    19int main() {20    std::string text→ gamma = "gamma";2122    std::unique_ptr<Note> note = std::make_unique<Note>(textgamma);
  2. Note(std::string value)

    9public:10    Note(std::string valuegamma) {11        text = valuegamma;12    }
  3. note ← (empty)

    22std::unique_ptr<Note> note→ (empty) = std::make_unique<Note>(textgamma);2324std::cout << "note=" << note(empty)->read() << std::endl;25std::cout << "owned=" << (note != nullptr) << std::endl;
  4. std::string read()

    14std::string read() {15    return textgamma;16}
  5. std::cout << "note=" << note->read() << std::endl;

    24    std::cout << "note=" << note(empty)->read() << std::endl;25    std::cout << "owned=" << (note(empty) != nullptr) << std::endl;26    return 0;27}
    outputnote=gamma
    owned=1

One Owner

  1. std::make_unique<Note>(text) creates one Note.
  2. note is the only owner of that object.
  3. note->read() uses the object while it is owned.
  4. At the end of main, note goes out of scope and deletes the object.
note -> Note("alpha")
end of scope -> note releases Note

Exercise: unique_ptr.cpp

Create a unique_ptr owner for a small object, read through it, and print whether it is non-null