dimanche 13 mars 2022

Why `shared_ptr

Since the constructor of std::shared_ptr is marked as explicit one, so expressions like auto p = std::make_shared<int>(1); p = new int(6); is wrong.

My question is that why std::make_shared<int>(1); p = nullptr; compiles?

Here is the aforementioned code snippet:

#include <memory>
#include <iostream>

int main()
{
    auto p = std::make_shared<int>(1);

    //p = new int(6);

    p = nullptr;

    if(!p)
    {
        std::cout << "not accessable any more" << std::endl;
    }

    p.reset();
}

Such code is seen at std::shared_ptr: reset() vs. assignment

Aucun commentaire:

Enregistrer un commentaire