C++ 声明其大小值来自 const 数组的数组

标签 c++ arrays constants constexpr compile-time-constant

我正在尝试定义一个堆栈 c 样式数组,其大小取自 const 数组并且在编译时已知。

const int size[2]={100, 100};
int ar[size[0]]; //error: expression must have a constant value

它失败了。如何修复?

最佳答案

“其大小取自 const 数组且在编译时中已知的数组”

使用 C++11,您可以:

constexpr int size[2]={100, 100};//size[0] 是编译时常量

使用-std=c++11-std=c++0x编译

关于C++ 声明其大小值来自 const 数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18984971/

相关文章:

c++ - 我如何从 C++ 中的静态线程入口点函数调用其他函数?

清除控制台时 C++ 闪烁

Python win32com 常量

PHP 等效于命名空间 [C/C++] 中的枚举

c++ - 观察者模式中的 Const-correct 通知程序

c++ - 通过 CDC COM 端口到 Arduino 的 Windows COM 通信

c++ - 是否有支持 g++ 或 clang 内存驻留编译的实用程序?

php - 在 PHP 中使用数组作为数组的键

arrays - 将 array<int^>^ 转换为 int*

java - java中 double 的选择排序