【发布时间】:2012-03-24 16:42:03
【问题描述】:
我正在尝试将堆栈上的固定大小分配给整数数组
#include<iostream>
using namespace std;
int main(){
int n1 = 10;
const int N = const_cast<const int&>(n1);
//const int N = 10;
cout<<" N="<<N<<endl;
int foo[N];
return 0;
}
但是,这会在我使用N 定义固定error C2057: expected constant expression 的最后一行出现错误。
但是,如果我将N 定义为const int N = 10,则代码编译得很好。
我应该如何将n1 转换为const int?
我试过了: const int N = const_cast<const int>(n1) 但这会出错。
编辑:我正在使用 MS VC++ 2008 来编译它...使用 g++ 编译得很好。
【问题讨论】:
标签: c++ stack integer constants