既然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 回答2k 阅读✓ 已解决
2 回答3.9k 阅读✓ 已解决
2 回答3.2k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
1 回答2.7k 阅读✓ 已解决
3 回答3.4k 阅读
1 回答1.6k 阅读✓ 已解决
我的理解:
constexpr
只是限制为纯函数;效果和内联是一样的,所以编译器不需要知道参数是什么