From 8d78feaa44f276b0f9c63b7148787f7fddf2ff41 Mon Sep 17 00:00:00 2001 From: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Date: Fri, 12 Jul 2024 21:13:03 +0800 Subject: [PATCH] gh-121647: Define _Py_TYPEOF macro on more compilers Extend the _Py_TYPEOF macro to use __typeof__() with MSVC 17.9 and newer, or decltype() under C++11 and newer. --- Include/pyport.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Include/pyport.h b/Include/pyport.h index 73a3e6cdaf09200..03f9b869bc82b55 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -538,12 +538,15 @@ extern "C" { // // Example: _Py_TYPEOF(x) x_copy = (x); // -// On C23, use typeof(). Otherwise, the macro is only defined -// if GCC or clang compiler is used. +// On C23, use typeof(). Otherwise __typeof__() if on GCC, clang or +// MSVC 17.9 and newer. Else if on C++11 or newer, decltype() is used. #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 202311L # define _Py_TYPEOF(expr) typeof(expr) -#elif defined(__GNUC__) || defined(__clang__) +#elif defined(__GNUC__) || defined(__clang__) || \ + (defined(_MSC_VER) && _MSC_VER >= 1939) # define _Py_TYPEOF(expr) __typeof__(expr) +#elif defined(__cplusplus) && __cplusplus >= 201103L +# define _Py_TYPEOF(expr) decltype(expr) #endif