c++11 - CUDA 8.0 : Compile Error with Template Friend in Namespace

标签 c++11 templates compiler-errors cuda nvcc

我注意到以下代码使用g++/clang++-3.8进行编译,但不能使用nvcc进行编译:

#include <tuple>   // not used, just to make sure that we have c++11
#include <stdio.h>

namespace a {
template<class T>
class X {
  friend T;
};
}

我收到以下编译错误:
/usr/local/cuda-8.0/bin/nvcc -std=c++11 minimum_cuda_test.cu
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
minimum_cuda_test.cu:7:10: error: ‘T’ in namespace ‘::’ does not name a type
   friend T;

有趣的是,这适用于nvcc:

#include <tuple>   // not used, just to make sure that we have c++11
#include <stdio.h>

template<class T>
class X {
  friend T;
};

这是编译器中的错误吗?我以为nvcc会在内部使用g++或clang作为编译器,所以我很困惑为什么这会与我的本地编译器一起使用而不与nvcc一起使用。

最佳答案

在这两种情况下,代码都是由g++编译的。但是,将.cu文件传递给nvcc时,会将代码传递给CUDA C++前端,然后再传递给主机编译器。查看带有gcc 4.8的CUDA 8,我发现代码已经从

namespace a {
template<class T>
class X {
  friend T;
};
}


namespace a {
template< class T>
class X {
friend ::T;
};

您可以看到,前端已用等效项替换了模板化的 friend ,但是使用了前置的匿名命名空间,这破坏了编译。我不是C++语言律师,但是在我看来,这似乎是CUDA前端的错误。我建议将其报告给NVIDIA。

关于c++11 - CUDA 8.0 : Compile Error with Template Friend in Namespace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47030180/

相关文章:

c++ - C++11 中由 std::tuple_size 终止的递归可变参数模板

c++ - 奇怪的 constexpr typeid 错误

c++ - 有条件地启用非模板函数c++

c++ - 使用类模板参数比使用实例变量有什么好处?

c++ - gcc 和 clang 是否没有正确推断模板别名类型?

c++ - 编译期间未识别的引用错误

c++ - 在 C++11 中使用 auto

c++ - 在模板参数的方法中添加类型转换时出现 clang 错误

algorithm - 遗传算法中的错误( Octave )

linux - 我正在尝试编写一个 shell 命令来查找并编译所有 C 程序