3 ms·
> C++ also has 'constexpr if' FTR since 1.46.0, Rust also has if inside const contexts (as well as loop and match, but no for loops as they are trait based and
by est31 4y ago
> C++ also has 'constexpr if'
FTR since 1.46.0, Rust also has if inside const contexts (as well as loop and match, but no for loops as they are trait based and traits in const are still being developed): https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1460-2020-08-27 https://github.com/rust-lang/rust/blob/master/RELEASES.md#ve...
- cyber_kinetist 4y agoThat isn't the same as 'constexpr if'. In C++ you can use 'constexpr if' even in non-constexpr/consteval contexts in order to control code generation, like for example: template<class T> auto foo() { if constexpr(compile_time_compute_value_changing_depending_on_T<T>()) { return Type1(); else { return Type2(); } } You can even change the return type of a function depending on the template parameter! Such is the power of duck-typed templates (as opposed to a strict trait system that Rust has)
- est31 4y agoOh I see that's a way different feature then, that Rust indeed doesn't have. You could in theory build a proc macro that supports a meta-language, but it would not be able interact with any of Rust's type system. Rust doesn't even allow _, the equivalent of auto, for return types of functions at all, even though it has a lot of type deduction (which is I think a good decision because it makes type deduction local, so you know which types you are dealing with by looking at the function header).