class.h: avoid C99 designated initializers in MRB_MT_ENTRY

Older C++ compilers (notably gcc 4.x) do not support C99 struct field
designators (.func = ...) even as an extension, which blocks building
mruby when it is included from a C++ translation unit under such
toolchains.

Reorder union mrb_mt_ptr so that mrb_func_t is the first member, and
switch MRB_MT_ENTRY to positional aggregate initialization.  Both are
compatible with pre-C99 / pre-C++20 compilers.

Fixes #6789

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2026-04-15 07:31:11 +09:00
parent d65f7a9c5a
commit a33ccd67b1
+6 -3
View File
@@ -100,10 +100,13 @@ void mrb_mc_clear_by_class(mrb_state *mrb, struct RClass* c);
typedef int (mrb_mt_foreach_func)(mrb_state*,mrb_sym,mrb_method_t,void*);
MRB_API void mrb_mt_foreach(mrb_state*, struct RClass*, mrb_mt_foreach_func*, void*);
/* ROM method table types for static method registration */
/* ROM method table types for static method registration.
* NOTE: `func` is kept as the first union member so that positional
* aggregate initialization in MRB_MT_ENTRY works without C99
* designated initializers (required for legacy C++ compilers). */
union mrb_mt_ptr {
const struct RProc *proc;
mrb_func_t func;
const struct RProc *proc;
};
/* entry combining function pointer, symbol key, and flags */
@@ -128,7 +131,7 @@ typedef struct mrb_mt_tbl {
/* ROM table entry: 3rd param is MRB_ARGS_*() optionally OR'd with MRB_MT_PRIVATE. */
#define MRB_MT_ENTRY(fn, sym, flags) \
{ { .func = (fn) }, (sym), (flags) | MRB_MT_FUNC }
{ { (fn) }, (sym), (flags) | MRB_MT_FUNC }
#define MRB_MT_ASPEC(flags) ((mrb_aspec)((flags) & 0xffffff))
/* "removed" tombstone: MRB_MT_FUNC flag set with NULL function pointer.