3 ms·
In Rust you can implement Add for all kinds of combinations of types. I.e. when one of the parameters moves it's ownership you can just perform the operation in
by panzi 3y ago
In Rust you can implement Add for all kinds of combinations of types. I.e. when one of the parameters moves it's ownership you can just perform the operation in-place in that parameter and return it. So you can eliminate temporaries at compile time. It means much more work in the library, but the library user can just write A + B + C.
So Rust has that and C++ has template specialization, const if, and variadic templates. Would be cool to have all in one language.
- menaerus 3y agoBut we already do. > So you can eliminate temporaries at compile time. > ... but the library user can just write A + B + C ... > So Rust has that. Eliminating temporaries in C++ is also done in compile-time through expression templates or more generally through TMP. I'm pretty sure Rust type system is incapable to express many (?) of the TMP things from C++ - it does not even support partial specializations to begin with so it's hard to imagine that you can do a lot of TMP gymnastics. I think that this is an area where it is generally accepted that Rust is lacking against C++.
- panzi 3y agoYeah, I was listing a lot of things that C++ can express that Rust can't. But C++ doesn't have lifetimes so you can't express those. And you can use that to eliminate temporaries. But if you say that you can also somehow do that with template meta programming then great!