Home >Backend Development >C++ >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
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!