如果我写下面的CUDA代码:
a.cu(8): warning: statement is unreachable
detected during instantiation of "void foo<N>() [with N=5U]"
(12): here
我收到编译器警告:
#include <cstdio>
template <unsigned N>
void foo()
{
std::printf("In kernel foo() with N = %u\n", N);
if (N < 10) { return; }
std::printf("Wow, N is really high!\n");
/* a whole lot of code here which I don't want to indent */
}
int main() {
foo<5>();
foo<20>();
return 0;
}
我感觉&#34;我不应该收到此警告,因为无法访问的代码仅对模板参数的某些值无法访问。如果我写&#34; CPU等价物&#34;,可以这么说:
-Wall
并使用gcc(5.4.0)构建它 - 即使我使用if (not (N < 10)) {
printf("Wow, N is really high!\n");
/* a whole lot of code here which I don't want to indent */
}
进行编译,我也不会收到任何警告。
现在,我可以通过编写
来规避这一点if (not (N < 10)) {
return;
}
else {
printf("Wow, N is really high!\n");
/* a whole lot of code here which I don't want to indent */
}
但我宁愿避免不得不改变我的逻辑来跳过nvcc&#34; hoop&#34;。我也可以写
[super touchesEnded:touches withEvent:event];
但是 - 我不想缩进所有代码(同样的问题可能会再次出现,在else块中需要更多的缩进。
我能做些什么吗?此外,这不是一个错误&#34;或者我应该报告错误的错误?
答案 0 :(得分:0)
怎么样:
template<unsigned N, bool>
struct FooImpl
{
static void foo()
{
std::printf("In kernel foo() with N = %u\n", N);
}
};
template<unsigned N>
struct FooImpl<N, false>
{
static void foo()
{
std::printf("In kernel foo() with N = %u\n", N);
std::printf("Wow, N is really high!\n");
/* a whole lot of code here which I don't want to indent */
}
};
template <unsigned N>
__global__ void foo()
{
FooImpl<N, N < 10>::foo();
}