既然constexpr是编译期已经确定了,为什么sth(int n)函数带变量n依然可以正常编译运行呢? 因为n是变量,编译器也不知道n是多少,应该无法编译通过才对。
constexpr int sth(int n) {
return 1+n;
}
int main() {
int a = 10;
int res = sth(a);
printf("res=%d\n", res);
}
//输出:11
既然constexpr是编译期已经确定了,为什么sth(int n)函数带变量n依然可以正常编译运行呢? 因为n是变量,编译器也不知道n是多少,应该无法编译通过才对。
constexpr int sth(int n) {
return 1+n;
}
int main() {
int a = 10;
int res = sth(a);
printf("res=%d\n", res);
}
//输出:11
3 回答1.3k 阅读✓ 已解决
1 回答1k 阅读✓ 已解决
4 回答832 阅读
1 回答905 阅读
1 回答940 阅读
1 回答707 阅读
1 回答809 阅读
我的理解:
constexpr
只是限制为纯函数;效果和内联是一样的,所以编译器不需要知道参数是什么