tag:blogger.com,1999:blog-87523202233301283802023-11-15T07:09:39.957-08:00C++ & BoostBlog about C++ and Boost libraries.Anonymoushttp://www.blogger.com/profile/13740808336152664145noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-8752320223330128380.post-42206644788490211402009-02-12T06:46:00.000-08:002009-02-16T11:52:18.082-08:00boost::bind<div>The first Boost library you should know how to use is boost::bind. Bind allows you to bind arguments to to function calls. It creates a functor (function object) that automatically can call the function associating it with the arguments. </div><div><br /></div><div>For example, if we have the following function:</div><div><br /></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">int f(int x, int y, int z) { return x + y + z; }</span></span></div><div><br /></div><div>And then we do:</div><div><br /></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">cout << bind(&f, 3, 4, 5)();</span></span></div><div><br /></div><div>This will execute f() with arguments 3, 4, and 5.</div><div><br /></div><div>We can also create functors that have arguments. These arguments will be pased to "placeholders" inside bind:</div><div><br /></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">cout << boost::bind(&f, _1, 10, _2)(3, 9);</span></span></div><div><br /></div><div>Will call f(3, 10, 9). We can choose the position of the arguments arbitrarily. </div><div><br /></div><div>Bind also works with member functions:</div><div><br /></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">struct Test</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">{</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;"> int w;</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;"> Test(int w) : w(w) {}</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;"> int f(int x, int y, int z) </span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;"> { </span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;"> return x + y + z + w; </span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;"> }</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">};</span></span></div><div><span class="Apple-style-span" style="font-size:small;"><br /></span></div><div><span class="Apple-style-span" style="font-size:small;"><br /></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">Test test(30);</span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;"><br /></span></span></div><div><span class="Apple-style-span" style="font-family:'courier new';"><span class="Apple-style-span" style="font-size:small;">cout << boost::bind(&Test::f, ref(test), _3, _2, _1)(10, 20, 30);</span></span></div><div><br /></div><div>will call test.f(30, 20, 10).</div><div><br /></div><div><br /></div><div><br /></div><div>Here, the second argument of bind must be the function's object.</div><div><br /></div><div>Since bind creates a copy of its arguments, we use boost::ref to wrap the object test so bind can store just a reference of the object. If we didn't do this, a copy of test held by the created functor. </div><div><br /></div><div><br /></div><div><br /></div><div>You can use bind with functors as well.</div>Anonymoushttp://www.blogger.com/profile/13740808336152664145noreply@blogger.com0tag:blogger.com,1999:blog-8752320223330128380.post-31241550522235814032009-02-09T05:38:00.000-08:002009-02-12T06:44:13.261-08:00Welcome!<span class="Apple-style-span" style="font-family:'lucida grande';">Hello People. This is my C++ & Boost blog. I'll be posting interesting things about C++ and the Boost Libraries. I hope you can find it interesting. Enjoy!<br /><br /></span>Anonymoushttp://www.blogger.com/profile/13740808336152664145noreply@blogger.com0