【问题标题】:Variadic template and template template (probably a compiler issue related to CUDA)可变参数模板和模板模板(可能是与CUDA相关的编译器问题)
【发布时间】:2016-04-27 15:54:52
【问题描述】:

我尝试编写小模板函数make,以方便构造一些与Functor结构相同的函子。对于具有一个参数的仿函数,如下所示可以正常工作:

template <class ARG1>
struct Functor{
    Functor(ARG1 x){ }
};

template <template <class> class FCT, class ARG>
FCT<ARG> make(ARG arg){
    return FCT<ARG>(arg);
}

void main(){
    int a = 5;
    make<Functor>(a);
}

现在我尝试扩展 make 以将其用于具有任意数量参数的仿函数,例如现在的两个参数仿函数 Functor:

template <class ARG1, class ARG2>
struct Functor{
    Functor(ARG1 x, ARG2 y){ }
};

template <template <class, class...> class FCT, class... ARG>
FCT<ARG...> make(ARG... arg){
    return FCT<ARG...>(arg...);
}

void main(){
    int a = 5;
    double b = 6;
    make<Functor>(a, b);
}

这不再起作用了,编译器说:

basic.cpp(199):错误:没有函数模板“make”的实例匹配 参数列表 参数类型是:(int, double)

老实说,我不知道这里出了什么问题。我没有看到与第一个示例在概念上的差异。我需要做什么才能让它发挥作用?

一些进一步的调查,基于 cmets:

使用g++-4.8.3直接编译代码为main.cpp时效果很好:

/path/to/g++-4.8.3 -std=c++11 main.cpp

使用g++-4.8.3通过nvcc编译代码为main.cu时会报错:

/path/to/cuda/cuda-6.5/bin/nvcc main.cu -o experiments_cuda -O0 -g -ccbin=/path/to/g++-4.8.3 --compiler-options='-std=c++11' -std=c++11

此外,将代码编译为main.cppvia nvcc:

/path/to/cuda/cuda-6.5/bin/nvcc main.cpp -o experiments_cuda -O0 -g -ccbin=/path/to/g++-4.8.3 --compiler-options='-std=c++11' -std=c++11

C++ 11 标志似乎传递正确 - 如果我删除它们,我会得到更多错误。

【问题讨论】:

  • 在修复了常见的新手错误之后似乎work as indented。
  • @KerrekSB:我在这里感觉自己像个盲人……你改变了什么? void main 到 int main 是我发现的......(我应该是一个真正的程序员,只是区分代码)。
  • 我可能也切换到了 C++11 编译器 :-)
  • @KerrekSB:是的,它似乎以一种特殊的方式与编译器相关。我将其编辑到问题中。
  • 传递-cuda,让我们看看nvcc给g++提供了什么。

标签: c++ templates c++11 variadic-templates


【解决方案1】:

也许是nvcc 需要--std c++11 而不是--std=c++11?

【讨论】:

  • 两者似乎相等。 (我不想投反对票,但这真的不是答案。)
猜你喜欢
  • 2012-11-05
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
  • 1970-01-01
  • 2011-09-28
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
相关资源
最近更新 更多