【问题标题】:class has no member "second_argument_type" when using std::ref() with nvcc [duplicate]将 std::ref() 与 nvcc 一起使用时,类没有成员“second_argument_type”[重复]
【发布时间】:2016-07-22 09:36:10
【问题描述】:

我正在尝试使用 cuda 7.5.18 和 gcc 5.4.0 编译带有 nvccc++ 文件,但遇到了错误。举个简单的例子:

#include <thrust/functional.h>

struct test_struct {
    //..
}

void f(test_struct& a) {
    //..
}

int main() {
    test_struct a;
    std::function<void()> bound_f = std::bind(f, std::ref(a));
}

使用nvcc -c -std=c++11 test.cu -o test.o 编译此代码会导致以下错误输出:

/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include/c++/functional(78): error: class "test_struct" has no member "result_type"
          detected during:
            instantiation of class "std::_Maybe_get_result_type<_Functor, void> [with _Functor=test_struct]" 
(86): here
            instantiation of class "std::_Weak_result_type_impl<_Functor> [with _Functor=test_struct]" 
(184): here
            instantiation of class "std::_Weak_result_type<_Functor> [with _Functor=test_struct]" 
(264): here
            instantiation of class "std::_Reference_wrapper_base_impl<true, true, _Tp> [with _Tp=test_struct]" 
(283): here
            instantiation of class "std::_Reference_wrapper_base<_Tp> [with _Tp=test_struct]" 
(399): here
            instantiation of class "std::reference_wrapper<_Tp> [with _Tp=test_struct]" 
test.cu(14): here

/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include/c++/functional(266): error: class "test_struct" has no member "argument_type"
          detected during:
            instantiation of class "std::_Reference_wrapper_base_impl<true, true, _Tp> [with _Tp=test_struct]" 
(283): here
            instantiation of class "std::_Reference_wrapper_base<_Tp> [with _Tp=test_struct]" 
(399): here
            instantiation of class "std::reference_wrapper<_Tp> [with _Tp=test_struct]" 
test.cu(14): here

/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include/c++/functional(267): error: class "test_struct" has no member "first_argument_type"
          detected during:
            instantiation of class "std::_Reference_wrapper_base_impl<true, true, _Tp> [with _Tp=test_struct]" 
(283): here
            instantiation of class "std::_Reference_wrapper_base<_Tp> [with _Tp=test_struct]" 
(399): here
            instantiation of class "std::reference_wrapper<_Tp> [with _Tp=test_struct]" 
test.cu(14): here

/usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include/c++/functional(268): error: class "test_struct" has no member "second_argument_type"
          detected during:
            instantiation of class "std::_Reference_wrapper_base_impl<true, true, _Tp> [with _Tp=test_struct]" 
(283): here
            instantiation of class "std::_Reference_wrapper_base<_Tp> [with _Tp=test_struct]" 
(399): here
            instantiation of class "std::reference_wrapper<_Tp> [with _Tp=test_struct]" 
test.cu(14): here

5 errors detected in the compilation of "/tmp/tmpxft_00003b29_00000000-7_test.cpp1.ii".

我找不到与error: ... has no member "second_argument_type" 等行相关的任何 nvcc,所以我完全一无所知。显然std::function 的班级成员不知何故找不到(请参阅here)。

我可以做些什么来解决这个问题?

【问题讨论】:

  • 您的代码没有多大意义。如果您使用的是 function 和 C++ 标准库中的朋友,为什么在任何地方都没有 #include &lt;functional&gt;?你为什么要导入thrust::functional?两者不可互换......
  • 包含在 中。此外,这只是一个 MWE,我并没有声称代码有意义。我只是在尝试基于 cuda 编译更大的项目时遇到这些错误。
  • 无论我对您的代码做什么(缺少明显的错误),我都无法使用 g++ 4.8 和 CUDA 7.5 发布工具包重现任何类型的编译错误。这看起来像是 CUDA 不支持 gcc 5 的简单案例。升级到 CUDA 8 RC 候选版本或降级到支持的编译器
  • 嗨,这可能是原因,如 here 所示,cuda 需要 gcc-4.x。我会尝试。谢谢!
  • 证明编译可以使用 gcc-4.9。如果您可以将您的评论作为答案,我会接受。

标签: c++ cuda nvcc


【解决方案1】:

错误原因尚不清楚,但您可以使用 lambda 轻松解决:

std::function<void()> bound_f = [&a](){ return f(a); };

【讨论】:

  • 不幸的是,当我尝试编译一个我无法轻易更改的大项目时出现上述错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 2021-08-29
  • 1970-01-01
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多