Getting a boost::shared_ptr for this

You can derive from enable_shared_from_this and then you can use “shared_from_this()” instead of “this” to spawn a shared pointer to your own self object. Example in the link: #include <boost/enable_shared_from_this.hpp> class Y: public boost::enable_shared_from_this<Y> { public: shared_ptr<Y> f() { return shared_from_this(); } } int main() { shared_ptr<Y> p(new Y); shared_ptr<Y> q = p->f(); assert(p == … Read more

more spirit madness – parser-types (rules vs int_parser) and meta-programming techniques

I’m not so sure I get the full extent of the question, but here are a few hints The line commented with // THIS is what I need to do. compiles fine with me (problem solved? I’m guessing you actually meant assigning a parser, not a rule?) Initialization of function-local static has been defined to … Read more

What are the advantages of boost::noncopyable

I see no documentation benefit: #include <boost/noncopyable.hpp> struct A : private boost::noncopyable { }; vs: struct A { A(const A&) = delete; A& operator=(const A&) = delete; }; When you add move-only types, I even see the documentation as misleading. The following two examples are not copyable, though they are movable: #include <boost/noncopyable.hpp> struct A … Read more

How to use boost bind with a member function

Use the following instead: boost::function<void (int)> f2( boost::bind( &myclass::fun2, this, _1 ) ); This forwards the first parameter passed to the function object to the function using place-holders – you have to tell Boost.Bind how to handle the parameters. With your expression it would try to interpret it as a member function taking no arguments. … Read more

Example to use shared_ptr?

Using a vector of shared_ptr removes the possibility of leaking memory because you forgot to walk the vector and call delete on each element. Let’s walk through a slightly modified version of the example line-by-line. typedef boost::shared_ptr<gate> gate_ptr; Create an alias for the shared pointer type. This avoids the ugliness in the C++ language that … Read more

how boost::function and boost::bind work

boost::function allows anything with an operator() with the right signature to be bound as the parameter, and the result of your bind can be called with a parameter int, so it can be bound to function<void(int)>. This is how it works (this description applies alike for std::function): boost::bind(&klass::member, instance, 0, _1) returns an object like … Read more

What is the performance overhead of std::function?

There are, indeed, performance issues with std:function that must be taken into account whenever using it. The main strength of std::function, namely, its type-erasure mechanism, does not come for free, and we might (but not necessarily must) pay a price for that. std::function is a template class that wraps callable types. However, it is not … Read more