Given this code, would that be undefined behaviour?
struct S {
void foo() {
// non-static member function, but not using "this"
}
};
int main() {
S* s = new S;
std::thread t(std::bind(&S::foo, s));
delete s;
}
Point here being: "this" is not used at all inside foo(), but it is a normal member function. Am I allowed to destroy the instance although another thread is still running on it?
(I know that for all practical purposes, this would not crash. But is it undefined behaviour?)
And second: what if foo() uses member variables, but I guarantee (via locking, events or whatever) that it won't access anything from "this" from the time onwards the instance is deleted?