diff options
author | Dong-hee Na <donghee.na@python.org> | 2022-05-21 14:52:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-21 14:52:45 (GMT) |
commit | dd923c57252ee90306a6fe7401ffc5a9cdc91485 (patch) | |
tree | c8fee21e08f8951a4947d359cb9cef6fa3626b1b /Include | |
parent | d9a48d2b413194f36111aa54df331d60c5bfde3f (diff) | |
download | cpython-dd923c57252ee90306a6fe7401ffc5a9cdc91485.zip cpython-dd923c57252ee90306a6fe7401ffc5a9cdc91485.tar.gz cpython-dd923c57252ee90306a6fe7401ffc5a9cdc91485.tar.bz2 |
[3.11] GH-92898: Make _Py_Cast C++ version compatible with cast operator (gh-92951) (gh-93049)
Diffstat (limited to 'Include')
-rw-r--r-- | Include/pyport.h | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/Include/pyport.h b/Include/pyport.h index 614a278..086ed42 100644 --- a/Include/pyport.h +++ b/Include/pyport.h @@ -26,8 +26,32 @@ // _Py_CAST(const PyObject*, expr) fails with a compiler error. #ifdef __cplusplus # define _Py_STATIC_CAST(type, expr) static_cast<type>(expr) -# define _Py_CAST(type, expr) \ - const_cast<type>(reinterpret_cast<const type>(expr)) + +extern "C++" { +namespace { +template <typename type, typename expr_type> +inline type _Py_reinterpret_cast_impl(expr_type *expr) { + return reinterpret_cast<type>(expr); +} + +template <typename type, typename expr_type> +inline type _Py_reinterpret_cast_impl(expr_type const *expr) { + return reinterpret_cast<type>(const_cast<expr_type *>(expr)); +} + +template <typename type, typename expr_type> +inline type _Py_reinterpret_cast_impl(expr_type &expr) { + return static_cast<type>(expr); +} + +template <typename type, typename expr_type> +inline type _Py_reinterpret_cast_impl(expr_type const &expr) { + return static_cast<type>(const_cast<expr_type &>(expr)); +} +} // namespace +} +# define _Py_CAST(type, expr) _Py_reinterpret_cast_impl<type>(expr) + #else # define _Py_STATIC_CAST(type, expr) ((type)(expr)) # define _Py_CAST(type, expr) ((type)(expr)) |