@@ -331,21 +331,29 @@ PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
331331 * `dst` points to a valid object.
332332 *
333333 * Temporary variables are used to only evaluate macro arguments once and so
334- * avoid the duplication of side effects. _Py_TYPEOF() or memcpy() is used to
335- * avoid a miscompilation caused by type punning. See Py_CLEAR() comment for
336- * implementation details about type punning.
334+ * avoid the duplication of side effects. _Py_TYPEOF(), C++ auto, or memcpy()
335+ * is used to avoid a miscompilation caused by type punning. See Py_CLEAR()
336+ * comment for implementation details about type punning.
337337 *
338338 * The memcpy() implementation does not emit a compiler warning if 'src' has
339339 * not the same type than 'src': any pointer type is accepted for 'src'.
340340 */
341341#ifdef _Py_TYPEOF
342342#define Py_SETREF (dst, src ) \
343343 do { \
344- _Py_TYPEOF(dst)* _tmp_dst_ptr = &(dst); \
344+ _Py_TYPEOF (&( dst)) _tmp_dst_ptr = &(dst); \
345345 _Py_TYPEOF (dst) _tmp_old_dst = (*_tmp_dst_ptr); \
346346 *_tmp_dst_ptr = (src); \
347347 Py_DECREF (_tmp_old_dst); \
348348 } while (0 )
349+ #elif defined(__cplusplus) && (__cplusplus >= 201103L || _MSVC_LANG >= 201103L)
350+ #define Py_SETREF (dst, src ) \
351+ do { \
352+ auto _tmp_dst_ptr = &(dst); \
353+ auto _tmp_old_dst = (*_tmp_dst_ptr); \
354+ *_tmp_dst_ptr = (src); \
355+ Py_DECREF (_tmp_old_dst); \
356+ } while (0 )
349357#else
350358#define Py_SETREF (dst, src ) \
351359 do { \
@@ -363,11 +371,19 @@ PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
363371#ifdef _Py_TYPEOF
364372#define Py_XSETREF (dst, src ) \
365373 do { \
366- _Py_TYPEOF(dst)* _tmp_dst_ptr = &(dst); \
374+ _Py_TYPEOF (&( dst)) _tmp_dst_ptr = &(dst); \
367375 _Py_TYPEOF (dst) _tmp_old_dst = (*_tmp_dst_ptr); \
368376 *_tmp_dst_ptr = (src); \
369377 Py_XDECREF (_tmp_old_dst); \
370378 } while (0 )
379+ #elif defined(__cplusplus) && (__cplusplus >= 201103L || _MSVC_LANG >= 201103L)
380+ #define Py_XSETREF (dst, src ) \
381+ do { \
382+ auto _tmp_dst_ptr = &(dst); \
383+ auto _tmp_old_dst = (*_tmp_dst_ptr); \
384+ *_tmp_dst_ptr = (src); \
385+ Py_XDECREF (_tmp_old_dst); \
386+ } while (0 )
371387#else
372388#define Py_XSETREF (dst, src ) \
373389 do { \
0 commit comments