3 ms·
>Things that are hard in Rust, are even harder in C and C++ This is absolutely not true. Something hard in Rust but almost trivial in C++ (via fold expressions
by logicchains 4y ago
>Things that are hard in Rust, are even harder in C and C++
This is absolutely not true. Something hard in Rust but almost trivial in C++ (via fold expressions): mapping or folding a function or operator over a tuple of heterogeneous types. E.g. to sum over a tuple of arbitrary numeric types:
auto mySum = std::apply([](auto... x){ return (x + ...); }, myTuple);
- Shikadi 4y agoWouldn't this be a nearly identical call, but specifying the add trait? I'm still learning so I'm not sure if I'm missing something
- crabbygrabby 4y agoThis is a pretty weird example. Why do you want to sum over a tuple of different data types in the first place? This kind of has a code smell I can't put my finger on... Fwiw rust doesn't let you dynamically index tuples, because rusts' take on tuples is different. Should your tuple actually be a struct with a "sum" method on it? Imo probably. Or should you have your numeric types be the same type for better memory layout, maybe... Just because another language let's you do something doesn't mean you should do it. Kind of like in js, does it make sense to divide a number by a string(not sure if you can do this but guessing it's valid js), probably not, is it a feature or a bug, you decide. Should this be part of say the kotlin standard, probably not.