【发布时间】:2012-11-17 11:25:41
【问题描述】:
可能重复:
Programmatically create static arrays at compile time in C++
是否可以在编译时初始化以下数组?
template<int n> void
foo()
{
static int pairs[2*n]; // = {0,0, 1,1, ..., n-1,n-1}
for (int i = 0; i < n; i++)
{
pairs[2*i] = pairs[2*i+1] = i;
}
do_something_with_pairs(pairs);
}
(我在 Xcode 4.5 上使用 Clang,所以 C++11 可以)
【问题讨论】:
-
循环使用超出数组末尾的索引 => 未定义行为。
-
抱歉,我现在修好了。
-
我想知道有多少人把他们的工作放在一边,努力解决这个问题。这个问题问得好。但我要放弃了。
标签: c++ templates template-meta-programming