summaryrefslogtreecommitdiffstats
path: root/Include
diff options
context:
space:
mode:
authorserge-sans-paille <serge.guelton@telecom-bretagne.eu>2022-05-21 13:16:37 (GMT)
committerGitHub <noreply@github.com>2022-05-21 13:16:37 (GMT)
commit5b71b519f966e1017c868ea2b27c61a5eac38c1f (patch)
tree6344abc2bcf797a03a0a2490541327af41e563ba /Include
parent14c0d33016a967a98155f2e1615660e9328aef5d (diff)
downloadcpython-5b71b519f966e1017c868ea2b27c61a5eac38c1f.zip
cpython-5b71b519f966e1017c868ea2b27c61a5eac38c1f.tar.gz
cpython-5b71b519f966e1017c868ea2b27c61a5eac38c1f.tar.bz2
GH-92898: Make _Py_Cast C++ version compatible with cast operator (gh-92951)
Diffstat (limited to 'Include')
-rw-r--r--Include/pyport.h28
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))