Consteval Functions and Template Parameters Dependent on Function Arguments
In C 17, a template parameter cannot depend on a function argument because the compiler still needs to generate runtime instructions for non-constexpr functions, even if they are evaluated at compile-time.
C 20 Consteval Functions
C 20 introduces consteval functions, which must be evaluated at compile-time, removing the runtime constraint. However, the question remains: does this mean template parameters can now depend on function arguments?
No Allowances for Dependent Template Parameters
Despite the introduction of consteval functions, the answer is no. The paper acknowledges that parameters are not intended to be treated as core constant expressions. This is due to potential typing discrepancies, as demonstrated by the example in the paper:
consteval int sqrsqr(int n) { return sqr(sqr(n)); // Not a constant-expression at this point, } // but that's okay.
Therefore, function parameters will never be considered constant expressions, preventing template parameters from being dependent on them.
The above is the detailed content of Can Template Parameters Depend on Function Arguments in C 20 Consteval Functions?. For more information, please follow other related articles on the PHP Chinese website!