在尝试书上错误代码后,C++代码编译不报错。
constexpr函数的实参如果是非字面值不应该报错吗,这里是变量我以我理解应该报错的呀。搞不懂
constexpr int fact(int=5);
int main(){
int i=4;
cout<<fact(i)<<endl;
return 0;
}
constexpr int fact(int i){ //阶乘
int temp=1;
while(i!=1){
temp *= i; //简化版:temp*=i--;
--i;
}
return temp;
}
运行成功不报错。
想了解原因。