如果我编写以下CUDA代码:

#include <stdio.h>

template <unsigned N>
__global__ void foo()
{
    printf("In kernel foo() with N = %u\n", N);
    if (N < 10) { return; }
    printf("Wow, N is really high!\n");
    /* a whole lot of code here which I don't want to indent */
}

int main() {
    foo<5><<<1,1>>>();
    foo<20><<<1,1>>>();
    return 0;
}


我收到编译器警告:

a.cu(8): warning: statement is unreachable
          detected during instantiation of "void foo<N>() [with N=5U]"
(12): here


我“觉得”我不应该收到此警告,因为只有模板参数的某些值才无法访问代码。如果我写“ CPU等效”,可以这么说:

#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;
}


并使用gcc(5.4.0)构建它-即使使用-Wall进行编译,我也不会收到任何警告。

现在,我可以通过写来规避

if (not (N < 10)) {
    printf("Wow, N is really high!\n");
    /* a whole lot of code here which I don't want to indent */
}


但是我宁可避免不得不颠倒自己的逻辑以跳过nvcc的“篮球”。我也可以写

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 */
}


但是-我不想缩进所有代码(并且可能再次发生相同的问题,在else块中甚至需要更多缩进。

有什么我可以做的吗?另外,这不是“错误”,还是我应该报告为错误的错误功能?

最佳答案

关于什么:

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();
}

关于c++ - 如何避免由于编译时计算的值导致的“无法到达的语句”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43158760/

10-11 23:14
查看更多