Home > Backend Development > C++ > Can Template Parameters Depend on Function Arguments in C 20 Consteval Functions?

Can Template Parameters Depend on Function Arguments in C 20 Consteval Functions?

DDD
Release: 2024-10-23 20:37:30
Original
249 people have browsed it

Can Template Parameters Depend on Function Arguments in C  20 Consteval Functions?

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.
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template