Skip to content

lifetime of local variable #433

@Dharmesh946

Description

@Dharmesh946

Channel

C++ weekly

Topics

Object lifetime
Return value
This SO thread is a rough sum-up of the issue: https://stackoverflow.com/questions/73481385/how-to-determine-when-local-variables-are-destroyed

What is a function local object lifetime?
How does it depend on it being part of the return expression?
How does it depend on the returned type (plain value, l/rvalue reference)?
How does it depend on the call site (here is a very contrived example : https://godbolt.org/z/dEqoMP3ca where a temporary is returned as rvalue reference, binding it to a plain object leads to dangling reference and UB while binding it to a const& expand the lifetime of the bound objet; NB I designed the foo function to avoid (N)RVO).

I found many related questions on SO but it's always focused on a very special case (as imposed by SO) making it difficult to have the "big picture".

Length

I honestly don't know if the subject can be treated in less than 10 minutes or not

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions