[3.11] GH-92898: Make _Py_Cast C++ version compatible with cast operator (gh-92951) (gh-93049)

This commit is contained in:
Dong-hee Na 2022-05-21 23:52:45 +09:00 committed by GitHub
parent d9a48d2b41
commit dd923c5725
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View file

@ -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))