diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 62768adcef..b1365ec1e3 100644 --- a/include/pybind11/numpy.h +++ b/include/pybind11/numpy.h @@ -43,6 +43,14 @@ class array; // Forward declaration PYBIND11_NAMESPACE_BEGIN(detail) +#ifndef PYBIND11_CONSTINIT +# if __cplusplus >= 202002L +# define PYBIND11_CONSTINIT constinit +# else +# define PYBIND11_CONSTINIT +# endif +#endif + // Main author of this class: jbms@ template class LazyInitializeAtLeastOnceDestroyNever { @@ -247,7 +255,7 @@ struct npy_api { }; static npy_api &get() { - static LazyInitializeAtLeastOnceDestroyNever api_init; + PYBIND11_CONSTINIT static LazyInitializeAtLeastOnceDestroyNever api_init; return api_init.Get(lookup); } @@ -685,7 +693,8 @@ class dtype : public object { private: static object &_dtype_from_pep3118() { - static detail::LazyInitializeAtLeastOnceDestroyNever imported_obj; + PYBIND11_CONSTINIT static detail::LazyInitializeAtLeastOnceDestroyNever + imported_obj; return imported_obj.Get([]() { return detail::import_numpy_core_submodule("_internal").attr("_dtype_from_pep3118"); });