std::函数参数不完整,不允许使用类型

std::function parameter is incomplete type not allowed

本文关键字:不允许 类型 函数 参数 std      更新时间:2023-10-16

我正试图将lambda分配给std::function,如下所示:

std::function<void>(thrust::device_vector<float>&) f;
f = [](thrust::device_vector<float> & veh)->void
{   
    thrust::transform( veh.begin(), veh.end(), veh.begin(), tanh_f() );
};

我得到一个错误:

 error: incomplete type is not allowed
 error: type name is not allowed

我认为它指的是thrust::device_vector<float>。我尝试了类型命名和类型定义参数:

typedef typename thrust::device_vector<float> vector;
std::function<void>(vector&) f;
f = [](vector & veh)->void
{   
    thrust::transform( veh.begin(), veh.end(), veh.begin(), tanh_f() );
};

没有用。然而,如果我只使用lambda(没有std::function),它就可以工作:

typedef typename thrust::device_vector<float> vector;
auto f = [](vector & veh)->void
{   
    thrust::transform( veh.begin(), veh.end(), veh.begin(), tanh_f() );
};

我错过了什么?PS:我正在使用nvcc release 6.5, V6.5.12g++ (Debian 4.8.4-1) 4.8.4 编译

您使用了错误的语法。

尝试std::function<void(thrust::device_vector<float>&)> f;而不是

std::function<void(thrust::device_vector<float>&)> f;声明了一个类型为std::function<void(thrust::device_vector<float>&)>的变量,它是一个接受thrust::device_vector<float>&并返回void 的函数对象

g++会给您不完整的类型错误,因为std::function<void>不是有效的模板实例化。

clang++会给您一个更好的错误消息,告诉您std::function<void>() f;是一个无效的变量声明:

main.cpp:11:28: error: expected '(' for function-style cast or type construction
    std::function<void>(int) f;
                        ~~~^
1 error generated.