Open
Description
[[gsl::Pointer]] can be used to diagnose a limited set of dangling pointer issues. However, it does not seem to preserve lifetime info through copies; see https://godbolt.org/z/vfjojda7K. Since the docs say a class so annotated "behaves like a pointer into the owner", it seems like copied "pointer"s should also dangle when the original owner is destroyed.
#include <optional>
struct [[gsl::Owner]] T {};
struct [[gsl::Pointer]] S {
S(const T&);
};
S f1() {
T t;
return S(t); //-Wreturn-stack-address, yay
}
S f2() {
T t;
S s(t);
return s; // No -Wreturn-stack-address, boo
}
std::optional<S> f3() {
T t;
return S(t); // No -Wreturn-stack-address, boo
}