
How can I use boost:bind library to bind to constructor of a class. Right now, I have code like this: class build { public: Rect* operator()(string& str) { return new Rect(str); } }; void TestBlockData::buildRectList( vector<string>& inputs, RectList& bdl) { transform(inputs.begin(), inputs.end(), bdl.begin(), build()); } I wonder if I can get rid of the whole "build class" by using boost::bind library. Thank you.

Meryl Silverburgh wrote:
How can I use boost:bind library to bind to constructor of a class.
Right now, I have code like this: class build { public:
Rect* operator()(string& str) { return new Rect(str); }
};
void TestBlockData::buildRectList( vector<string>& inputs, RectList& bdl) {
transform(inputs.begin(), inputs.end(), bdl.begin(), build()); }
I wonder if I can get rid of the whole "build class" by using boost::bind library.
No, you can't; boost::bind can only adapt existing function objects into new function objects, but as a general rule it can't create function objects from scratch. The Lambda library has new_ptr<Rect>() that may work for you, though: http://www.boost.org/doc/html/lambda/le_in_details.html#lambda.construction_...

Thanks. But I have the following compiler error. Thanks for any idea to fix it. code: #include "boost/lambda/bind.hpp" #include "boost/lambda/lambda.hpp" using namespace std; using namespace boost::lambda; transform(inputs.begin(), inputs.end(), bdl.begin(), _1 = bind(new_ptr<Rect>())); ../TestBlockData.cpp: In member function 'void TestBlockData::buildBlockDataList(std::vector<std::string, std::allocator<std::string> >&, BlockDataList&)': ../TestBlockData.cpp:119: error: '_1' was not declared in this scope ../TestBlockData.cpp:119: error: 'new_ptr' was not declared in this scope ../TestBlockData.cpp:119: error: expected primary-expression before '>' token ../TestBlockData.cpp:119: error: expected primary-expression before ')' token On 2/14/06, Peter Dimov <pdimov@mmltd.net> wrote:
Meryl Silverburgh wrote:
How can I use boost:bind library to bind to constructor of a class.
Right now, I have code like this: class build { public:
Rect* operator()(string& str) { return new Rect(str); }
};
void TestBlockData::buildRectList( vector<string>& inputs, RectList& bdl) {
transform(inputs.begin(), inputs.end(), bdl.begin(), build()); }
I wonder if I can get rid of the whole "build class" by using boost::bind library.
No, you can't; boost::bind can only adapt existing function objects into new function objects, but as a general rule it can't create function objects from scratch. The Lambda library has new_ptr<Rect>() that may work for you, though:
http://www.boost.org/doc/html/lambda/le_in_details.html#lambda.construction_...
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Meryl Silverburgh wrote:
Thanks. But I have the following compiler error. Thanks for any idea to fix it.
code: #include "boost/lambda/bind.hpp" #include "boost/lambda/lambda.hpp"
using namespace std;
using namespace boost::lambda;
transform(inputs.begin(), inputs.end(), bdl.begin(), _1 = bind(new_ptr<Rect>()));
transform( inputs.begin(), inputs.end(), bdl.begin(), new_ptr<Rect>() ); should be enough.
../TestBlockData.cpp: In member function 'void TestBlockData::buildBlockDataList(std::vector<std::string, std::allocator<std::string> >&, BlockDataList&)': ../TestBlockData.cpp:119: error: '_1' was not declared in this scope
That's odd.
../TestBlockData.cpp:119: error: 'new_ptr' was not declared in this scope
Looks like the compiler can't find new_ptr. You probably need to #include "boost/lambda/construct.hpp".
participants (2)
-
Meryl Silverburgh
-
Peter Dimov