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

Can Consteval Functions Legalize Function Parameters in Template Parameters Dependent on Function Arguments?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-24 03:53:30492browse

Can Consteval Functions Legalize Function Parameters in Template Parameters Dependent on Function Arguments?

Consteval Functions: Limitations on Template Parameters Dependent on Function Arguments

In C 17, defining constexpr functions that rely on template parameters dependent on function arguments was prohibited. While consteval functions in C 20 promise to enforce compile-time evaluation, does this mean that such code can now be legalized?

Question:

consteval int foo(int i) { return std::integral_constant::value; }. Will this code be valid in C 20?

Answer:

No.

Explanation:

Despite the introduction of consteval functions, the underlying principle remains that non-template function definitions have a single point of type resolution. Legalizing the proposed code would have implications for the One Definition Rule (ODR). Additionally, the C 20 paper explicitly states that function parameters will not be treated as core constant expressions due to potential type mismatches.

As a result, function parameters cannot be directly used as constant expressions within template parameter dependencies, even in consteval function contexts.

The above is the detailed content of Can Consteval Functions Legalize Function Parameters in Template Parameters Dependent on Function Arguments?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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