Skip to content

Liftetimes: shared_ptr questions #523

Open
@jrmuizel

Description

@jrmuizel

I'm trying to better understand how the lifetime analysis deals with shared_ptrs. I was not able to figure out why the following program would not compile with lifetime analysis turned on.

using namespace std;
struct B;

struct A
{
        shared_ptr<B> b;
        int k;
};

struct B
{
        unique_ptr<A> a;
};

void foo(A* a)
{
        a->b->a = nullptr;
        a->k = 5;
}

void d(A* a, shared_ptr<B> b)
{
        a->b = b;
}
void g(B* b, unique_ptr<A> a)
{
        b->a = move(a);
}

int main()
{
        shared_ptr<B> b = make_shared<B>();
        auto a = make_unique<A>();

        a->b = b;
        d(a.get(), b);
        g(b.get(), move(a));
        foo(b->a.get());
}

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions