[cfe-dev] Question about -Wlifetime and unique pointers

Adrian via cfe-dev cfe-dev at lists.llvm.org
Sat Nov 2 13:09:08 PDT 2019


Hello,

I have a question about the experimental -Wlifetime flag/the Lifetime
profile of the C++ core guidelines
(https://github.com/isocpp/CppCoreGuidelines/blob/master/docs/Lifetime.pdf).

Consider the following example: https://godbolt.org/z/L1o35w
x is a unique pointer, y = x.get()
x is passed by non-const reference to a function, which can modify it
(which it does).
Dereferencing y gives a warning (3rd tab), as the compiler has
detected the dependency of y on x, and the pointer held by x may be
deleted after calling the function.

However, dereferencing x does not give a warning (and it indeed crashes if run).
It seems to me that the compiler already knows that x is invalid, as
it knows y is invalid from x.
Is there a fundamental reason why it doesn't give a a warning/why it's
hard to in this case?

Kind regards,
Adrian



More information about the cfe-dev mailing list