C++ std::函数参数类型不完整不允许
我试图将lambda分配给C++ std::函数参数类型不完整不允许,c++,c++11,lambda,C++,C++11,Lambda,我试图将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() ); }; 我认为它
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() );
};
我认为它指的是推力::设备_向量。我尝试了类型命名和类型定义参数:
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() );
};
我错过了什么?
注:我正在使用nvcc 6.5版、V6.5.12版和g++(Debian 4.8.4-1)4.8.4版编译,您使用了错误的语法 尝试
std::函数f代码>取而代之
std::函数f
声明一个类型为std::function
的变量,该变量是一个函数对象,它接受推力::设备向量&
并返回void
由于std::function
不是有效的模板实例化,g++会给出不完整的类型错误
clang++提供了一条更好的错误消息,告诉您std::function()f代码>是无效的变量声明:
main.cpp:11:28: error: expected '(' for function-style cast or type construction
std::function<void>(int) f;
~~~^
1 error generated.
main.cpp:11:28:错误:函数样式转换或类型构造应为“(”
std::函数(int)f;
~~~^
生成1个错误。
@Alex没关系。你犯了一个常见的错误,我认为这个问题和答案会让犯类似错误的其他人受益。这实际上是合法的语法吗?int(int)fp;
声明一个指向函数的指针,该函数接受int并返回int吗?@immibis no;尽管int(int)
是一种类型(函数类型,而不是指针类型!),变量声明必须使用中缀符号:(又名“声明遵循用法”)。我们将函数称为fp(5);
因此声明必须是int(*fp)(int)
,其中*
是使其成为指针类型所必需的,括号是防止*
与返回值关联所必需的。类似于我们无法编写int[5]arr;
@M.M这是我的想法;此答案的先前版本声称std::function(推力::设备向量)f;
是函数指针的有效声明。
typedef typename thrust::device_vector<float> vector;
auto f = [](vector & veh)->void
{
thrust::transform( veh.begin(), veh.end(), veh.begin(), tanh_f() );
};
main.cpp:11:28: error: expected '(' for function-style cast or type construction
std::function<void>(int) f;
~~~^
1 error generated.