当我使用boost::bind声明为const和非const的方法名称时,我会遇到模糊错误,例如
boost::bind( &boost::optional<T>::get, _1 )
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
我正在尝试std::find_if通过boost::bind与boost::contains(来自boost/algoritm/string库)一起使用来创建谓词.以下片段显示了我试图完成此任务的两种方式.
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <iostream>
#include <string>
int main(int argc, char** argv) {
std::string s1("hello mom");
std::string s2("bye mom");
boost::function<bool (std::string, std::string)> f = &boost::contains<std::string, std::string>;
std::cout << s1 << " contains " << "hello, " << std::boolalpha << f(s1, "hello") << std::endl;
std::cout << s2 << " contains " << "hello, " << std::boolalpha << f(s2, "hello") << std::endl;
boost::function<bool (std::string)> contain_hello = boost::bind(boost::contains<std::string, std::string>, _1, std::string("hello"));
std::cout << …Run Code Online (Sandbox Code Playgroud) 如果我想参数化创建一个对象,我当然可以创建一个在特定类上调用new并传出指针的函数.我想知道是否可以跳过该步骤并将函数指针传递给new运算符本身.
我正在编写一个使用的应用程序boost::asio.Asio async_receive(或async_read)总是使用boost::bind给出的回调对象显示:
boost::asio::async_read(socket_,
boost::asio::buffer(read_msg_.data(),
chat_message::header_length),
boost::bind(&chat_session::handle_read_header,
shared_from_this(),
boost::asio::placeholders::error));
Run Code Online (Sandbox Code Playgroud)
这非常好,但我不想在每次调用回调后重新创建绑定对象.相反,我想在我的类的构造函数中创建对象,并将其赋予async_receive.
问题是,我不知道如何将该对象声明为类成员.我所知道的只是汽车,它显然不会作为班级成员.
class Whatever
{
public:
Whatever()
{
functor = boost::bind(&Whatever::Callback);
}
private:
void Callback()
{
boost::asio::async_read(socket_,
boost::asio::buffer(read_msg_.data(),
chat_message::header_length),
functor);
}
?? functor; // How do I declare this?
...
};
Run Code Online (Sandbox Code Playgroud)
注意:这可能是过早优化,但我仍然想知道如何在没有auto的情况下声明绑定对象.
说我有这样的功能:
void someFunction(const ExpensiveObjectToCopy&);
Run Code Online (Sandbox Code Playgroud)
如果我将boost :: function输出,那么该函数将在其闭包中存储自己的克隆副本:
boost::function<void()> f = boost::bind(someFunction, x); // <-- f saves a copy of x
Run Code Online (Sandbox Code Playgroud)
现在,如果我开始传递f,boost :: function复制构造函数每次都会复制该对象,还是每个函数共享相同的闭包?(就像这样)
boost::function<void()> f2 = f;
callSomeFunction(f);
etc.
Run Code Online (Sandbox Code Playgroud) 我发现自己创造了一堂课
template <typename T> struct invoker {
void operator()(T& it) const {it();}
};
Run Code Online (Sandbox Code Playgroud)
所以我可以传递一个invoker<foo>到要调用的东西(这是不是我的控制之下),invoker<foo>::operator()(foo&)就可以反复用不同的foo情况下,得到它的呼叫转发到foo的foo::operator()()方法.
我知道它只有几行,但这似乎是STL的功能或boost::bind某种方式已经提供的那种东西.除非我看不到诀窍,如果有的话.(我确定我不是第一个使用非常类似的东西的人;它有名字吗?)
希望同时多次调用函数.我希望使用线程来调用一个能充分利用机器功能的功能.这是一台8核机器,我的要求是使用10%到100%或更高的机器CPU.
我的要求是使用boost类.有什么方法可以使用boost线程或线程池库来完成这个任务吗?还是其他一些方法呢?
另外,如果每次必须使用不同的参数调用多个函数(使用单独的线程),最好的方法是什么?[使用提升或不使用提升]以及如何?
#include <iostream>
#include <fstream>
#include <string.h>
#include <time.h>
#include <boost/thread/mutex.hpp>
#include <boost/bind.hpp>
using namespace std;
using boost::mutex;
using boost::thread;
int threadedAPI1( );
int threadedAPI2( );
int threadedAPI3( );
int threadedAPI4( );
int threadedAPI1( ) {
cout << "Thread0" << endl;
}
int threadedAPI2( ) {
cout << "Thread1" << endl;
}
int threadedAPI3( ) {
cout << "Thread2" << endl;
}
int threadedAPI4( ) {
cout << "Thread3" << endl;
}
int main(int argc, char* argv[]) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试将boost :: signal的触发包装到boost :: bind对象中.所以我想要的是在调用boost :: function时用一些预先打包的参数调用信号.
我有的是这个:
boost::signals2::signal<void(int)> sig;
boost::function<void()> f = boost::bind(
&(sig.operator()), &sig, 10);
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我收到以下错误:错误:没有匹配函数调用bind(,...
我也试过这个:
boost::function<void()> f = boost::bind(
(void(boost::signals2::signal<void(int)>::*)(int))
&(sig.operator()), &sig, 10);
Run Code Online (Sandbox Code Playgroud)
但后来我得到了"没有上下文类型信息的重载函数的地址".
那么什么是正确的语法?
假设我有一个subscribe()调用的函数,它接受一个回调处理程序,当触发事件时将调用它.
现在,我有另一个版本,叫做subscribe2().一切都是相同的,除了触发时,它需要将其发布到事件队列.它是使用原始实现的subscribe(),带有一个名为helper的函数helper().它所做的只是将原始处理程序和任何其他参数绑定到仿函数中,然后调用postToEventQueue().
现在,我想知道是否有一种消除辅助函数的方法,因此subsribe2(),我可以以某种方式直接打包postToTaskQueue()函数和原始回调处理程序,并将其传递给subscribe().原因是我有很多不同的处理程序类型,并且在整个地方引入辅助函数是很乏味和累人的.毕竟,boost :: bind应该在给定原始函数的情况下返回一个新函数,对吧?我试图用boost :: bind直接生成辅助函数.
一种尝试就是说
subscribe(boost::bind(boost::bind(postToTaskQueue, boost::bind(_1, _2)), cb, _1));
Run Code Online (Sandbox Code Playgroud)
在subscribe2(),但它不起作用.有可能吗?
请参阅下面的详细示例代码.谢谢!
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
typedef boost::function<void(int)> SomeCallback;
typedef boost::function<void()> Task;
void handler(int i){
std::cout << "i=" << i <<std::endl;
}
void subscribe(SomeCallback cb)
{
cb(100); //just invoke the callback for simplicity
}
void postToTaskQueue(Task t)
{
t(); // just invoke the task for simplicity
} …Run Code Online (Sandbox Code Playgroud) boost-bind ×10
c++ ×10
boost ×3
templates ×2
boost-asio ×1
boost-lambda ×1
boost-thread ×1
functor ×1
new-operator ×1
overloading ×1
stl ×1