boost-bind

std::stringstream as parameter to a function

岁酱吖の 提交于 2019-12-07 12:55:04
问题 I have a std::vector<std::string> temp_results and I wish to use std::for_each to go through this vector and concatenate a string, so I concocted the following construction: std::stringstream ss; std::string res = std::for_each(temp_results.begin(), temp_results.end(), boost::bind(addup, _1, ss)); std::string addup(std::string str, std::stringstream ss) { ss << str; ss << ";"; return ss.str; } I get the following error, which is beyond my understanding: error C2475: 'std::basic_stringstream<

null pointer when getting function pointer using boost::function::target

て烟熏妆下的殇ゞ 提交于 2019-12-07 11:22:27
After reading this answer I thought I had a solution. At least the answer there is what I would like to do but I'm having a problem with the implementation. here is an outline of what I am trying to do typedef map<string, double*> myMap; typedef int (*ftwpt)(const char*, const struct stat*, int); typedef boost::function<int(const char*, const struct stat*, int)> MyFTWFunction; int myFunction(const char*, const struct stat*, int, myMap*); int main() { myMap m_map; char tmpdir[] = "/tmp/mytmp"; MyFTWFunction f = boost::bind(myFunction,_1,_2,_3, &m_map); ftwpt* fpt = f.target<ftwpt>(); if (fpt)

Is it possible to create a function pointer to the a function's `new` operator/constructor?

僤鯓⒐⒋嵵緔 提交于 2019-12-07 06:14:02
问题 If I were to wanted to parameterize creating an object, I could of course make a function which called new on a particular class and passed out a pointer. I am wondering if it's possible to skip that step and pass a function pointer to the new operator itself. 回答1: boost::lambda provides function wrappers for new and delete. These can be used to easily convert an new call into a function object. 回答2: operator new (as well as the other flavours) takes care of allocating memory but does not

Using for_each and boost::bind with a vector of pointers

痞子三分冷 提交于 2019-12-06 23:27:44
问题 I have a vector of pointers. I would like to call a function for every element, but that function takes a reference. Is there a simple way to dereference the elements? Example: MyClass::ReferenceFn( Element & e ) { ... } MyClass::PointerFn( Element * e ) { ... } MyClass::Function() { std::vector< Element * > elements; // add some elements... // This works, as the argument is a pointer type std::for_each( elements.begin(), elements.end(), boost::bind( &MyClass::PointerFn, boost::ref(*this), _1

sort using boost::bind

╄→尐↘猪︶ㄣ 提交于 2019-12-06 15:55:13
bool pred(int k, int l, int num1, int num2) { return (num1 < num2); } int main() { vector <int> nums; for (int i=50; i > 0; --i) { nums.push_back(i); } std::sort (nums.begin(), nums.end(), boost::bind(&pred, 5, 45)); } I am a boost newbie. I was learning to use boost::bind and I wanted to sort a vector of integers and get rid of all those elements in the vector that are greater than 45 and less than 5. Had a hard time doing it. Would be great if anyone could help me do it? The reason I am facing problem is because I am trying to get rid of a vector element while iterating through the vector to

How to avoid boost::phoenix when generating with boost::spirit::karma

戏子无情 提交于 2019-12-06 14:50:05
I'm a victim of error "LNK1179: invalid or corrupt file: duplicate COMDAT" and these sources lead me to believe that by not using phoenix I could avoid this error. (This is a follow-up to my previous question .) I want to replace boost::phoenix with something else. Maybe boost::bind but I don't see how I can give it access to karma::_val . The following code fails to compile on VC9 with error C2825: 'F': must be a class or namespace when followed by '::' #include <boost/config/warning_disable.hpp> #include <boost/foreach.hpp> #include <boost/assign/list_of.hpp> #include <boost/range/adaptors

“Interface” like semantics with boost::bind

你说的曾经没有我的故事 提交于 2019-12-06 12:56:36
I wanted to be able to have something like Java's interface semantics with C++. At first, I had used boost::signal to callback explicitly registered member functions for a given event. This worked really well. But then I decided that some pools of function callbacks were related and it made sense to abstract them and register for all of an instance's related callbacks at once. But what I learned was that the specific nature of boost::bind and/or taking the value of this seemed to make that break. Or perhaps it was just the fact that the add_listener(X &x) method declaration changed the code

Why does boost::bind store arguments of the type passed in rather than of the type expected by the function?

﹥>﹥吖頭↗ 提交于 2019-12-06 12:35:41
问题 I recently ran into a bug in my code when using boost::bind. From the boost::bind docs: The arguments that bind takes are copied and held internally by the returned function object. I had assumed that the type of the copy that was being held was based on the signature of the function. However, it is actually based on the type of the value passed in. In my case an implicit conversion was happening to convert the type used in the bind expression to the type received by the function. I was

Boost threads - passing parameters by reference

拜拜、爱过 提交于 2019-12-06 06:37:02
问题 My application has a section that resembles the following code void SomeClass::OtherMethod(std::vector<std::string>& g) { g.pushback("Something"); } void SomeClass::SomeMethod() { std::vector<std::string> v; boost::thread t(boost::bind(&SomeClass::OtherMethod,this,v) t.join(); std::cout << v[0]; //Why is this empty when the vector created on stack } I wanted to know why the vector v is empty when the vector is created on the stack and it works when it is created on the heap. I was expecting

Using boost::bind and boost::lambda::new_ptr to return a shared_ptr constructor

故事扮演 提交于 2019-12-06 04:05:45
Given a class A, class A { public: A(B&) {} }; I need a boost::function<boost::shared_ptr<A>(B&)> object. I prefer not to create an ad-hoc function boost::shared_ptr<A> foo(B& b) { return boost::shared_ptr<A>(new A(b)); } to solve my problem, and I'm trying to solve it binding lambda::new_ptr. boost::function<boost::shared_ptr<A> (B&)> myFun = boost::bind( boost::type<boost::shared_ptr<A> >(), boost::lambda::constructor<boost::shared_ptr<A> >(), boost::bind( boost::type<A*>(), boost::lambda::new_ptr<A>(), _1)); that is, I bind in two steps the new_ptr of a A and the constructor of shared_ptr.