boost::bind处理boost::shared_ptr与原始指针相同的方式.
QObject * object(new QObject);
boost::shared_ptr<QObject> sharedObject(new QObject);
bind(&QObject::setObjectName, object, _1)( "name" );
bind(&QObject::setObjectName, sharedObject, _1)( "name" );
Run Code Online (Sandbox Code Playgroud)
我希望有一个boost::bind处理QPointers作为原始指针指针.
QPointer<QObject> guardedObject(new QObject);
// i want to write it like this
bind(&QObject::setObjectName, guardedObject, _1)( "name" );
//now i have to do it like this
bind(&QObject::setObjectName, bind(&QPointer<QObject>::data, guardedObject), _1)( "name" );
Run Code Online (Sandbox Code Playgroud)
是否有人为此专业化QPointer?
如果不是,你知道从哪里开始或需要专门做什么,所以我可以自己做.
我似乎无法声明一个函数的泛型指针.
要调用这两个函数:
void myfunc1(std::string str)
{
std::cout << str << std::endl;
}
struct X
{
void f(std::string str){ std::cout<< str << std::endl;}
};
Run Code Online (Sandbox Code Playgroud)
和这两个函数调用者:
typedef void (*userhandler_t) (std::string);
struct example
{
userhandler_t userhandler_;
example(userhandler_t userhandler): userhandler_(userhandler){}
void call(std::string str)
{
userhandler_(str);
}
};
template<typename func_t>
void justfunc(func_t func)
{
func("hello, works!");
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用它们与boost :: bind来调用成员函数时,它们会给我编译错误.
这工作:
example e1(&myfunc1);
e1.call("hello, world!");
justfunc(&myfunc1);
Run Code Online (Sandbox Code Playgroud)
这不是:
X x;
example e2(boost::bind(&X::f, &x, _1));
e2.call("hello, world2!");
justfunc(boost::bind(&X::f, &x, _1));
Run Code Online (Sandbox Code Playgroud)
应该怎么做?
我必须将函数传递给指针.为此我正在使用boost :: function.对于不同的签名,捕获指针的函数被重载.例如:
void Foo(boost::function<int ()>) { ... }
void Foo(boost::function<float ()>) { ... }
void Foo(boost::function<double ()>) { ... }
Run Code Online (Sandbox Code Playgroud)
现在我想在那里传递一些类方法指针:
class test
{
public:
float toCall() { };
};
class Wrapper
{
Wrapper() {
test obj;
Foo(boost::bind(&test::toCall, this));
}
};
error: no matching function for call to ‘Foo(boost::_bi::bind_t<float, boost::_mfi::mf0<float, test>, boost::_bi::list1<boost::_bi::value<Wrapper*> > >)’
note: candidates are: Foo(boost::function<float()>&)
Run Code Online (Sandbox Code Playgroud) 假设我有方法:
void foo(const std::string& s);
Run Code Online (Sandbox Code Playgroud)
我可以创建boost :: function:
boost::function<void(const std::string&)> f = boost::bind(foo, temp);
Run Code Online (Sandbox Code Playgroud)
其中temp是char*,在f被调用之前被删除.
这与前一个问题有关:使用boost :: bind和boost :: function:检索绑定变量类型.
我可以绑定一个这样的函数:
在.h:
class MyClass
{
void foo(int a);
void bar();
void execute(char* param);
int _myint;
}
Run Code Online (Sandbox Code Playgroud)
在.cpp
MyClass::bar()
{
vector<boost::function<void(void)> myVector;
myVector.push_back(boost::bind(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* param)
{
boost::function<void(void)> f = myVector[0];
_myint = atoi(param);
f();
}
Run Code Online (Sandbox Code Playgroud)
但是如何绑定返回值呢?即:
在.h:
class MyClass
{
double foo(int a);
void bar();
void execute(char* param);
int _myint;
double _mydouble;
}
Run Code Online (Sandbox Code Playgroud)
在.cpp
MyClass::bar()
{
vector<boost::function<void(void)> myVector;
//PROBLEM IS HERE: HOW DO I BIND "_mydouble"
myVector.push_back(boost::bind<double>(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* …Run Code Online (Sandbox Code Playgroud) 我试图在std :: transform内使用boost :: bind连接两个字符串
假设我的类有两个方法来获取两个字符串(第一个和第二个),并且conatiner是字符串向量,我试图做如下
struct Myclass
{
std::string getFirstString() {return string1}
std::string getSecondString() {return string2}
private:
std::string string1;
std::string string2;
}
Myclass myObj;
std::vector<string > newVec;
std::vector<myObj> oldVec;
std::transform (oldVec.begin(), oldVec.end(), std::back_inserter(newVec), boost::bind(&std::string::append,boost::bind(&getFirstString, _1),boost::bind(&getSecondString, _1 ) ) );
Run Code Online (Sandbox Code Playgroud)
但是,我得到错误说
error: cannot call member function 'virtual const getSecondString() ' without object
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?
我有以下类型:
struct X { int x; X( int val ) : x(val) {} };
struct X2 { int x2; X2() : x2() {} };
typedef std::pair<X, X2> pair_t;
typedef std::vector<pair_t> pairs_vec_t;
typedef std::vector<X> X_vec_t;
Run Code Online (Sandbox Code Playgroud)
我需要初始化pairs_vec_t带有值的实例X_vec_t.我使用以下代码,它按预期工作:
int main()
{
pairs_vec_t ps;
X_vec_t xs; // this is not empty in the production code
ps.reserve( xs.size() );
{ // I want to change this block to one line code.
struct get_pair {
pair_t operator()( const X& value ) { …Run Code Online (Sandbox Code Playgroud) 我有以下代码,其中Boost.Local使用函数回调来加载mo文件.这个函数对我来说叫做findMo,我试图将它绑定到一个对象,这样我就可以保留我放在moFinder的私有成员中的副作用.
class moFinder
{
public:
moFinder(string const& wantedFormat)
: format(wantedFormat)
{
// ...
}
std::vector<char> findMo(std::string const& filePath, std::string const& encoding)
{
// ...
}
};
std::locale createLocale(string const& name, string const& customTranslation,
string const& path, string const& domain, string const& pathFormat)
{
// ...
namespace blg = boost::locale::gnu_gettext;
blg::messages_info info;
info.paths.push_back(path);
info.domains.push_back(blg::messages_info::domain(domain));
moFinder finder(pathFormat);
blg::messages_info::callback_type callbackFunc;
callbackFunc = boost::bind(moFinder::findMo, boost::ref(finder));
info.callback = callbackFunc;
// ...
}
Run Code Online (Sandbox Code Playgroud)
编译时我收到以下错误:
错误:无效使用非静态成员函数'std :: vector moFinder :: findMo(const std :: string&,const std :: string&)' …
我有一些代码在一个boost线程中运行,它修改了主线程处理的东西,这个东西不起作用,这是有意义的.
在android上我会有Handler一个消息队列,它将在主线程上执行我的代码,我可以将任何我想要的参数传递给这个处理程序.
我想用提升来做同样的事情
所以在我的主线程上我做了以下事情:
boost::thread workerThread(boost::bind(&SomeClass::pollService, this));
Run Code Online (Sandbox Code Playgroud)
我的pollService方法:
SomeClass::pollService()
{
//get some stuff from a web service
//parse the json response
//NEEDED part: call a function to be executed on the main thread and hand it some functions
}
Run Code Online (Sandbox Code Playgroud)
PS我看了很多io_service.post例子,我仍然不知道怎么做,而且我也读了一个说要用的答案,asio strand但我也无法理解.
有人可以为我愚蠢吗?请不要写那么抽象的东西,我不明白,我没有经验.谢谢
我是错误"LNK1179:无效或损坏的文件:重复COMDAT"的受害者,这些 来源让我相信,通过不使用phoenix我可以避免此错误.
(这是我之前的问题的后续行动.)我想boost::phoenix用其他东西替换.也许,boost::bind但我不知道如何让它访问karma::_val.
以下代码无法在VC9上编译
错误C2825:'F':当后跟'::'时必须是类或命名空间
#include <boost/config/warning_disable.hpp>
#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <string>
#include <list>
namespace karma = boost::spirit::karma;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
class Item
{
public:
typedef std::vector<int> Values;
Item(const std::string & i, const Values & v) : m_id(i), m_values(v) {}
std::string getId() const { …Run Code Online (Sandbox Code Playgroud) boost-bind ×10
c++ ×8
boost ×7
boost-asio ×1
boost-spirit ×1
boost-thread ×1
pointers ×1
qpointer ×1
qt ×1
stl ×1