3 ms·
Templates + RAII
by dman 8y ago
Templates + RAII
- Koshkin 8y agoAnd operator overloading: I want to write A + B instead of matrix_add(A, B).
- dymk 8y agoThis is actually one of the strongest arguments against writing performance sensitive code in C++. Because of operator overloading, I don't know if `A * B` is incurring a (nontrivial, arbitrary) function call. Maybe it's just normal scalar multiplication, maybe it's finding the dot product of two arbitrarily sized matrices.
- jcelerier 8y agoHaving used multiple maths libs over the year I have never been in a position when I did not know immediately what A * B does. Why are you writing it if you don't know what happens ? At which point in your life would you write A * B and not know if one of A or B is a matrix ?
- dymk 8y agoYou'll know what it does if you're the author. But code is read much more often than it's written, and it's easier to spot bugs and understand what each line of code does without operator overloading. Operator overloading obscures what the actual code is doing. Because of this, it's is generally not used in kernels, virtual machines, etc. As an example, if I give you this line of C++ code, and no other information (which would require further effort to determine, e.g. the types of identifiers), can you tell me if it's a function call, performs I/O, or does dynamic memory allocation? auto C = A + B;
- kllrnohj 8y agoHere's the equivalent C code: typeof(A) C = add(A, B) Now tell me if it involves a function call, performs I/O, or does dynamic memory allocation? And, of course, things like macros and typeof are totally used in kernels, virtual machines, etc... The ultimate obfuscation is alive & well in those areas. So no, the potential to do terrible things in operator overloading is not even remotely a concern there. No more than anything else.
- dymk 8y agoThat's not equivalent C code - because there is no equivalent C code. I can tell you, if I write this in C, it performs no I/O, memory allocation etc: typeof(A) C = A + B; I know for a fact that (modulo people doing incredibly stupid things with macros), that this is simple arithmetic addition. I don't know that for a fact in C++ - because of operator overloading.
- kllrnohj 8y ago> I know for a fact that (modulo people doing incredibly stupid things with macros), that this is simple arithmetic addition. I don't know that for a fact in C++ - because of operator overloading. The only way to know for a fact what happens in either C or C++ is to know the types of A & B, which you (seemingly intentionally) omitted. But if A & B are the types that can be added in C, then it's going to be literally identical in C++ with no chance of operator overloading. If A & B are not types that can be added in C, then you would see an add() function called instead and you'd be right back to not knowing what it does.
- dymk 8y agoYes, I am intentionally not looking at the types, because that's something that might not be incredibly apparent in C++ (or C). We're violently agreeing at this point. Yes, in C, this problem does not exist, because operator overloading does not exist. In C++, what `A+B` does is ambiguous without knowing 1) what are the types of A and B, and 2) what are all the possible `operator+` overloads that exist which may operate on A and B. It's a hidden complexity which doesn't make potentially expensive operations apparent, and kernels and VMs hate that kind of thing.
- gpderetta 8y agoAnd if you do matrix_add you do not know if it is a bunch of primitive operations or doing MPI operations.
- deleted 8y ago[deleted]
- deleted 8y ago[deleted]