Skip to content

Commit 6b37f77

Browse files
committed
zend_portability: Add C++ version to ZEND_CONTAINER_OF()
1 parent 3b9abe2 commit 6b37f77

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

Zend/zend_portability.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,21 @@ char *alloca();
387387
#define ZEND_ELEMENT_COUNT(m)
388388
#endif
389389

390-
#if __STDC_VERSION__ >= 202311L || ZEND_GCC_VERSION
390+
#if __cplusplus
391+
extern "C++" {
392+
# include <cstddef>
393+
template<typename T, typename M>
394+
const T* zend_container_of(const M *ptr, size_t offset) {
395+
return reinterpret_cast<const T*>(reinterpret_cast<const char*>(ptr) - offset);
396+
}
397+
template<typename T, typename M>
398+
T* zend_container_of(M *ptr, size_t offset) {
399+
return reinterpret_cast<T*>(reinterpret_cast<char*>(ptr) - offset);
400+
}
401+
402+
# define ZEND_CONTAINER_OF(ptr, Type, member) zend_container_of<Type, decltype(Type::member)>(ptr, offsetof(Type, member))
403+
}
404+
#elif __STDC_VERSION__ >= 202311L || ZEND_GCC_VERSION
391405
/* typeof is C23 or a GCC extension */
392406
# define ZEND_CONTAINER_OF(ptr, Type, member) \
393407
_Generic( \

0 commit comments

Comments
 (0)