3 ms·
That 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
by cyber_kinetist 4y ago
That 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).