diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 6c269adbe1..0d03f4fa60 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -3557,13 +3557,10 @@ typing::Iterator make_value_iterator(Type &value, Extra &&...extra) { template void implicitly_convertible() { - static int tss_sentinel_pointee = 1; // arbitrary value struct set_flag { - thread_specific_storage &flag; - explicit set_flag(thread_specific_storage &flag_) : flag(flag_) { - flag = &tss_sentinel_pointee; // trick: the pointer itself is the sentinel - } - ~set_flag() { flag.reset(nullptr); } + bool &flag; + explicit set_flag(bool &flag_) : flag(flag_) { flag_ = true; } + ~set_flag() { flag = false; } // Prevent copying/moving to ensure RAII guard is used safely set_flag(const set_flag &) = delete; @@ -3572,7 +3569,7 @@ void implicitly_convertible() { set_flag &operator=(set_flag &&) = delete; }; auto implicit_caster = [](PyObject *obj, PyTypeObject *type) -> PyObject * { - static thread_specific_storage currently_used; + thread_local bool currently_used = false; if (currently_used) { // implicit conversions are non-reentrant return nullptr; }