int nValue = 5;
auto fun = [&] ()->const int{ return nValue; };
const int nConst = fun();
cout << is_const<decltype(nConst)>::value << endl; // 1
int arr[nConst]; //编译失败,提示:表达式的计算结果不是常数
nConst = 5; // 编译失败,提示:不能给常量赋值
如上所示,lambda表达式返回的是常量, 使用is_cosnt验证也是常量,最后两个却编译失败,这从原理上怎么解析????
你这是运行态下才能知道const是多少,但数组大小必须编译态就知道是多少大小
可以参考这篇文章,讲的很细:https://blog.csdn.net/weixin_39790738/article/details/111109705: