C++ 可以在CUDA内核中使用std::conditional吗?

C++ 可以在CUDA内核中使用std::conditional吗?,c++,cuda,C++,Cuda,我正在寻找一个CUDA内核模板。这两个国家都不是特别乐于助人。在这个内核中,我希望有一个依赖于typename的typedef。我尝试了以下(假设/希望)它将类似于C++工作,但它没有。 #include <type_traits> // for std::conditional. Not sure if it's necessary #include <cuda_runtime.h> #include <cuComplex.h> template <

我正在寻找一个CUDA内核模板。这两个国家都不是特别乐于助人。在这个内核中,我希望有一个依赖于typename的typedef。我尝试了以下(假设/希望)它将类似于C++工作,但它没有。
#include <type_traits>  // for std::conditional. Not sure if it's necessary
#include <cuda_runtime.h>
#include <cuComplex.h>

template <typename fType>
__global__ void DummyKernel() {
    typedef std::conditional<sizeof(fType) == sizeof(double), cuDoubleComplex, cuFloatComplex>::type cfType;
}
#包含//用于std::conditional。不确定是否有必要
#包括
#包括
样板
__全局_uu; void DummyKernel(){
typedef std::conditional::type cfType;
}
这会产生错误

nontype "std::conditional<_Test, _Ty1, _Ty2>::type [with _Test=<expression>, _Ty1=cuDoubleComplex, _Ty2=cuFloatComplex]" is not a type name
nontype“std::conditional::type[with _Test=,_Ty1=cuDoubleComplex,_Ty2=cuFloatComplex]”不是类型名称

有没有办法做我想做的事?我正在使用CUDA 5.5和VS2012。

std::conditional
取决于模板参数
fType
,因此您必须在
typedef
之后放置
typename
关键字

请注意,对于那些希望在不依赖C++11的情况下获得相同结果的人(将C++11与CUDA结合使用目前可能不那么简单),您可以使用:

然后,你会得到:

DummyKernel<float>  ---> size = 8
DummyKernel<double> ---> size = 16
DummyKernel--->size=8
DummyKernel-->size=16

在CARULINUX上用CUDA 5.5和Boost 1.55进行测试。

在代码< TyPulf之后,您丢失了<代码>类型名称< /Cord>关键字。@ CassCracle我将要说我使用VS,并且它不符合标准,当我意识到英伟达编译器是分开的。我必须改掉我愚蠢的懒惰习惯。如果发布,我会接受这个答案。你能在CUDA内核中使用
std::conditional
吗?@JackOLantern我不知道。我从OP的评论中假设它对他有效…@JackOLantern所有的
std::conditional
代码都在头中,那么编译什么呢?
nvcc test.cu -o test -gencode arch=compute_30,code=sm_30
DummyKernel<float>  ---> size = 8
DummyKernel<double> ---> size = 16