【发布时间】:2019-10-15 23:00:27
【问题描述】:
我有一个 c 样式数组,其大小由 #define 定义,并且可以根据编译选项进行更改,例如
#if LINUX
# define SIZE 4
#else
# define SIZE 5
#endif
static int myArr[SIZE] = { /* ??? */ };
如何将整个数组初始化为非零值,例如所有42?
【问题讨论】:
-
@SamerTufail: memset 是运行时,OP 搜索编译时间我要离开!
-
是
#define V 42#define INIT4 V, V, V, V#define INIT6 INIT4 , V, V然后将#define INIT <XXX>放在那些#ifelses中太笨了吗?static int myArr[] = { INIT};不是很漂亮,但它完成了工作。 -
可以切换到
std::array吗? -
那么使用像Boost.PP这样的库怎么样?
-
我以为我有一种似曾相识的感觉stackoverflow.com/questions/54286610/…