Open
Description
For the code in https://godbolt.org/z/v919qqe6o:
struct [[gsl::Pointer]] function_ref {
template <typename Callable>
function_ref(Callable &&callable [[clang::lifetimebound]]) : ref(callable) {}
void (*ref)();
};
void f();
void g(function_ref &r) {
r = []() { f(); };
}
Clang trunk produces a warning:
warning: object backing the pointer r will be destroyed at the end of the full-expression [-Wdangling-assignment-gsl]
9 | r = []() { f(); };
Clang seems to consider the no-capture lambda as a temporary that will be destroyed at the end of the expression. I'm not a standards expert and I haven't found anything definitive through searching, but in practice no-capture lambdas will turn into ordinary functions, so there's no actual issue here. Should we actually be warning for this?
a3b4d91 is a commit that ran into this and changed the code slightly to avoid it (the code change made there would result in a stack-use-after-free were this an actual issue, but it avoids the warning). #126140 is a high-firing instance of this warning, and we'd appreciate some guidance.