Skip to content

Commit f07b28b

Browse files
committed
Add PYBIND11_CONSTINIT, but it does not work for the current use cases:
``` 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<npy_api> 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; | ^~~~~~~~~~~~ ```
1 parent e7be9c2 commit f07b28b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

include/pybind11/numpy.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class array; // Forward declaration
4343

4444
PYBIND11_NAMESPACE_BEGIN(detail)
4545

46+
#ifndef PYBIND11_CONSTINIT
47+
# if __cplusplus >= 202002L
48+
# define PYBIND11_CONSTINIT constinit
49+
# else
50+
# define PYBIND11_CONSTINIT
51+
# endif
52+
#endif
53+
4654
// Main author of this class: jbms@
4755
template <typename T>
4856
class LazyInitializeAtLeastOnceDestroyNever {
@@ -247,7 +255,7 @@ struct npy_api {
247255
};
248256

249257
static npy_api &get() {
250-
static LazyInitializeAtLeastOnceDestroyNever<npy_api> api_init;
258+
PYBIND11_CONSTINIT static LazyInitializeAtLeastOnceDestroyNever<npy_api> api_init;
251259
return api_init.Get(lookup);
252260
}
253261

@@ -685,7 +693,8 @@ class dtype : public object {
685693

686694
private:
687695
static object &_dtype_from_pep3118() {
688-
static detail::LazyInitializeAtLeastOnceDestroyNever<object> imported_obj;
696+
PYBIND11_CONSTINIT static detail::LazyInitializeAtLeastOnceDestroyNever<object>
697+
imported_obj;
689698
return imported_obj.Get([]() {
690699
return detail::import_numpy_core_submodule("_internal").attr("_dtype_from_pep3118");
691700
});

0 commit comments

Comments
 (0)