// Example program
#include <mutex>
struct A {
std::mutex m;
};
int main()
{
A* a = (A*) malloc(sizeof(A));
a->m = std::mutex();
}
this gives me
In function 'int main()':
11:9: error: use of deleted function 'std::mutex& std::mutex::operator=(const std::mutex&)'
In file included from 2:0:
/usr/include/c++/4.9/mutex:130:12: note: declared here
mutex& operator=(const mutex&) = delete;
^
how do I properly initialize he mutex m?
The reason I'm using malloc and not new is because I'm using this code inside a global replacement of new
and I don't want it to recurse back into the replacement.
operator new
. There is no guarantee that C++ features won’t themselves useoperator new
; if that happens the code can recourse infinitely. – Pete Becker Sep 9 '20 at 22:21thread_local bool already_in_new_;
variable and setting it to true true when inside new, but first checking if it is already new and in that case just returning malloc memory? would that be safe? – matthias_buehlmann Sep 10 '20 at 10:55