【发布时间】:2014-11-26 20:58:25
【问题描述】:
据我了解,不会采用在编译时评估为 false 的分支。在下面的代码中,编译器不应该丢弃包含导致错误的行的块吗?
#include <iostream>
template<int i1, int i2>
void TemplateFunc()
{
std::cout << i1 << std::endl;
if(i2 > 0)
{
std::cout << i2 << std::endl;
// The following causes an error
// "cannot allocate an array of constant size 0"
int someThing[i2] = {276};
std::cout << someThing[i2/2] << std::endl;
}
}
int main(int argc, char** argv)
{
TemplateFunc<1,2>();
TemplateFunc<3,0>();
return 0;
}
我已经在 VS 2012、g++(在 coliru 上使用“g++ -std=c++11 -O3 -Wall -pedantic -pthread main.cpp && ./a.out”)和使用 nvcc(使用cuda 内核中的类似代码)。
【问题讨论】:
标签: c++ templates optimization