From f07b28bda9f91fb723aa898a21c81b6dd6857072 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 9 Oct 2023 07:50:16 -0700 Subject: [PATCH] Add `PYBIND11_CONSTINIT`, but it does not work for the current use cases: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` g++ -o pybind11/tests/test_numpy_array.os -c -std=c++20 -fPIC -fvisibility=hidden -O0 -g -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wundef -Wnon-virtual-dtor -Wunused-result -Werror -isystem /usr/include/python3.11 -isystem /usr/include/eigen3 -DPYBIND11_STRICT_ASSERTS_CLASS_HOLDER_VS_TYPE_CASTER_MIX -DPYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD_IF_AVAILABLE -DPYBIND11_TEST_BOOST -Ipybind11/include -I/usr/local/google/home/rwgk/forked/pybind11/include -I/usr/local/google/home/rwgk/clone/pybind11/include /usr/local/google/home/rwgk/forked/pybind11/tests/test_numpy_array.cpp ``` ``` In file included from /usr/local/google/home/rwgk/forked/pybind11/tests/test_numpy_array.cpp:10: /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/numpy.h: In static member function ‘static pybind11::detail::npy_api& pybind11::detail::npy_api::get()’: /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/numpy.h:258:82: error: ‘constinit’ variable ‘api_init’ does not have a constant initializer 258 | PYBIND11_CONSTINIT static LazyInitializeAtLeastOnceDestroyNever api_init; | ^~~~~~~~ ``` ``` In file included from /usr/local/google/home/rwgk/forked/pybind11/tests/test_numpy_array.cpp:10: /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/numpy.h: In static member function ‘static pybind11::object& pybind11::dtype::_dtype_from_pep3118()’: /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/numpy.h:697:13: error: ‘constinit’ variable ‘imported_obj’ does not have a constant initializer 697 | imported_obj; | ^~~~~~~~~~~~ ``` --- include/pybind11/numpy.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/include/pybind11/numpy.h b/include/pybind11/numpy.h index 62768adce..b1365ec1e 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 @@ public: 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"); });