3 ms·
What you actually want is function overloading, which C can't support since C functions map directly to elf symbols, and all assembly functions are vararg with
by fctorial 6y ago
What you actually want is function overloading, which C can't support since C functions map directly to elf symbols, and all assembly functions are vararg with all arguments being WORDs. (That's how it is in elf, *nix and x64, don't know much about other platforms)
- FrozenVoid 6y ago_Generic macros provide similar functionality to function overload(which in C++ involves name mangling, that will never be accepted for C) https://en.cppreference.com/w/c/language/generic https://en.cppreference.com/w/c/language/generic
- lldbg 6y agoIn C++, name mangling is used to solve this issue
- mjburgess 6y agoYou don't need a vtable for overloading; that is just runtime polymorphism ("ad hoc polymorphism". Function overloading can be done compile time, eg., parametrically: add(3.0, 1.0) -> compile to add_float32_float32 add(1, 2) -> compile to add_int32_int32
- fctorial 6y agoAs I said, `add` function in C maps to a symbol named `add` in assembly. You're demangling the function name, which is what C++ does.
- mjburgess 6y agoSure, you're right. I guess we'd need C to have a special syntax to compile in something like a vtable add_f32_f32(x, y) calls add(MAGIC_NO, x, y) and add has a big switch on MAGIC_NO for selecting implementation... You'd need a keyword for this, "multi int add(int a, int b) ..."
- jcelerier 6y agosuch complications where this is what C++ gives you natively
- HexDecOctBin 6y agoNot necessarily, they could standardize it as a syntax like _Generic, to make it so that the user has to choose a unique name for each function, but then they can all be called by a common name.
- jws 6y agoClang has C function overloading. I use it in a reasonable sized code base. It really cleans up the source code, you end up skipping a lot of the type name or package name parts of function names which accumulate in larger projects leaving a much cleaner source to read. 100% recommend.