我注意到以下代码使用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/