5 ms·
C needs operator overloading. Something like this: https://github.com/FrozenVoid/Infodump-DB/blob/943900c2dbf2e9b41d00065c3ee29af8ccf8efbc/programming/c/_Operat
by FrozenVoid 6y ago
C needs operator overloading. Something like this:
https://github.com/FrozenVoid/Infodump-DB/blob/943900c2dbf2e9b41d00065c3ee29af8ccf8efbc/programming/c/_Operator.txt https://github.com/FrozenVoid/Infodump-DB/blob/943900c2dbf2e...
- pantalaimon 6y agoNo, I'd say that operators will always result in a primitive operation is actually a feature - everything more complex warrants a proper function.
- HexDecOctBin 6y agoI'd say they should probably go the other way, defining even built-in operators like: operator+(int a, int b) __gcc_internal_int_sum(a, b) This way, we can change (through overriding) the way these operations work (rounding, overflow, coercing, etc.), without having to define non-idiomatic functions to do so. A lot of undefined behavior can be made user-defined this way.
- gmueckl 6y agoThere should be a middle ground. The main issue with operator overloading is that every overload exists in the global scope. Thus, you get issues and surprises with unknown or competing overloads. Sometimes, a workable solution would be to have overloads restricted to a kind of named scope that the calling code can enter explicitly. Maybe something like operator_context(mz_vector_ops) { ... } which makes all operators defined in the context of my_vector_ops apply to expressions within the block, but nowhere else.
- FrozenVoid 6y agoConsider arbitrary precision, rationals, interval arithmethic, countless algebraic types, matrices, vectors with all of them requiring typing and composing long functions versus intuitive formula: mpz_mul(res,x,y); mpz_mul(res,res,z); vs x*y*z; ```
- jstimpfle 6y agoThe following style is much better: v3 r = Add_v3(v, w); v4 w = Scale_v3(r, 1.0); You can definitely write a little 3D graphics backend this way. C's lack of operator overloading can be annoying for some tasks, however I still like to use C for almost everything to this day, because of the lightness of going through old code and seeing instantly what happens - never have to run any type resolution in my head. And importantly, _knowing_ that I don't have to do this. Because I might almost never have to do this in cleanly written C++ code, but how do I know?
- jjoonathan 6y agoMath libraries are an exception. You want this: d + A*B*(c*(a*b)) Not this: vector_add(mv_multiply(matrix_multiply(A,B),vector_dot(a,b)*c),d)
- jcelerier 6y agoso are expression grammar libraries, etc
- wtetzner 6y agoIt'd be cool to have the Haskell feature of putting backticks around a binary function so you can use it infix, e.g.: ((A `mmul` B) `mvmul` (a `vdot` b) * c) `vadd` d Still not as nice as operators, but not terrible either.
- flohofwoe 6y agoYou can get this with clang's vector extension (https://clang.llvm.org/docs/LanguageExtensions.html#vectors-and-extended-vectors https://clang.llvm.org/docs/LanguageExtensions.html#vectors-...), and this would make a lot of sense to add to the C standard. IMHO vector math is about the only place where operator overloading is acceptable in C++.
- fctorial 6y agoWhat 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) ..."
- 6y ago