You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I took the liberty of analyzing this, and if you compile with -fsanitize=address, you get to see the problem also on clang++.
What happens is that in the pointer to member function constructor, the parameter f is a temporary, and obj_ will point to that temporary. The temporary is destroyed once the function_ref<> object has been constructed. When the call is made, obj_ refers to the corpse. Changing the program to avoid the temporary works:
intmain() {
auto mf = &S::foo;
tl::function_ref<int(S)> f = mf;
f({S});
}
This is the same undefined behaviour you would get when storing a temporary lambda and invoking the ref.
tl::function_ref<int(S)> f = [](){};
f();
Of course, the below snippet works and is why a function_ref is constructable from an rvalue in the first place.
// Any rvalue passed to this function will live as long as the function scopeintproxy(tl::function_ref<int(S)> f)
{
returnf(S{});
}
intmain()
{
returnproxy(&S::foo);
}
The text was updated successfully, but these errors were encountered: