Is it possible, at compile time, to determine if a call to a function (a constexpr function mainly) is compile-time evaluated and than just make another version of that function (like what a template does) with a different return type than the original runtime evaluated version ? How ?
constexpr decltype(auto) F(int n)
{
if consteval
{
return (int)3;
}
else
{
return (char)'c';
}
}
int main() {
int n;
cin >> n;
cout << typeid(decltype(F(4))).name() << endl;
cout << typeid(decltype(F(n))).name() << endl;
return 0;
}
error (gcc 12.2) : "inconsistent deduction for auto return type: 'int' and then 'char'"
Using if constexpr
doesn't give this error, because I think it just "deletes" a section of code (if it is false, the "if" section, otherwise the "else" section). But why doesn't if consteval
do the same thing ?
Is there an other way to do this ?