From bef55256b1a49f2b2b9397c61a8b945dd083a9ae Mon Sep 17 00:00:00 2001 From: Xuehai Pan Date: Thu, 8 Jan 2026 21:45:17 +0800 Subject: [PATCH] Appease MSVC Warning C4866: compiler may not enforce left-to-right evaluation order --- include/pybind11/pybind11.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index c457e149c1..02d2e72c2c 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1081,10 +1081,11 @@ class cpp_function : public function { dict kwargs; for (size_t i = 0; i < used_kwargs.size(); ++i) { if (!used_kwargs[i]) { - // Fetch value before indexing into kwargs to ensure well-defined - // evaluation order (MSVC C4866). - PyObject *const arg_in_arr = args_in_arr[n_args_in + i]; - kwargs[PyTuple_GET_ITEM(kwnames_in, i)] = arg_in_arr; + // Cast values into handles before indexing into kwargs to ensure + // well-defined evaluation order (MSVC C4866). + handle arg_in_arr = args_in_arr[n_args_in + i], + kwname = PyTuple_GET_ITEM(kwnames_in, i); + kwargs[kwname] = arg_in_arr; } } call.args.push_back(kwargs);