Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost::绑定为l值对象_C++_Functor_Boost Bind_Lvalue - Fatal编程技术网

C++ boost::绑定为l值对象

C++ boost::绑定为l值对象,c++,functor,boost-bind,lvalue,C++,Functor,Boost Bind,Lvalue,有没有办法做到这一点(微软VS 2008) boost::bind mybinder=boost::bind(/*这里绑定了一些东西*/); mybinder(/*这里的参数*/);//int foo(int){返回0;} boost::function a=boost::bind(f,_1); intfoo(int){return 0;} boost::function a=boost::bind(f,_1); 绑定返回未指定的类型,因此不能直接创建该类型的变量。但是,有一个类型模板boost

有没有办法做到这一点(微软VS 2008)

boost::bind mybinder=boost::bind(/*这里绑定了一些东西*/);
mybinder(/*这里的参数*/);//<代码>int foo(int){返回0;}
boost::function a=boost::bind(f,_1);
intfoo(int){return 0;}
boost::function a=boost::bind(f,_1);

绑定返回未指定的类型,因此不能直接创建该类型的变量。但是,有一个类型模板
boost::function
,可以为任何函数或函子类型构造。因此:

boost::function<int(int)> a = boost::bind(f, _1);
只要
f
intf(int)
就可以工作。该类型作为
std::function
与C++11闭包一起使用(以及
bind
,也被接受),被添加到C++11中:


但是如果您想传递它,
std::function
仍然很方便。

绑定返回未指定的类型,因此您不能直接创建该类型的变量。但是,有一个类型模板
boost::function
,可以为任何函数或函子类型构造。因此:

boost::function<int(int)> a = boost::bind(f, _1);
只要
f
intf(int)
就可以工作。该类型作为
std::function
与C++11闭包一起使用(以及
bind
,也被接受),被添加到C++11中:


但是如果你想传递它,
std::function
仍然很方便。

什么是“它不工作”呢?请接受前面的一些答案。你在这里已经一年多了,这足够多的时间来学习SO是如何工作的。“它不工作”是什么意思?请接受之前的一些答案。你已经在这里呆了一年多了,这足够多的时间来学习它是如何工作的。如果你在使用C++11和lambdas,你应该使用
std::function
@Tomalak:I扩展了C++11部分并切换到
std:
那里。如果你在使用C++11和lambdas,你应该使用
std::function
@Tomalak:I扩展了C++11部分,并切换到那里的
std::
,这也不对
a
可以直接等同于
foo
作为函数指针-这里不需要绑定。我得到了“错误C2079:'a'使用未定义的类'boost::function'”@fogbit:那么你没有
#包含
boost.function.@DeadMG:我知道这不是必需的,但也不是“不正确的”。我选择了最接近原始代码的解决方案。如果您想更进一步,OP可以将对
a
的整个调用替换为整数文本
0
。这也不对
a
可以直接等同于
foo
作为函数指针-这里不需要绑定。我得到了“错误C2079:'a'使用未定义的类'boost::function'”@fogbit:那么你没有
#包含
boost.function.@DeadMG:我知道这不是必需的,但也不是“不正确的”。我选择了最接近原始代码的解决方案。如果您想更进一步,OP可以将对
a
的整个调用替换为整数文本
0
int foo(int){return 0;}
boost::function<int(int)> a = boost::bind(f, _1);
boost::function<int(int)> a = boost::bind(f, _1);
boost::function<int(int)> a = &f;
std::function<int(int)> a = [](int i)->int { return f(i, 42); }
auto a = [](int i)->int { return f(i, 42); }