Using boost::future with “then” continuations

Boost.Thread comes in several versions of which you can choose via the BOOST_THREAD_VERSION macro. Currently, the default is 2. Up to version 2 of Boost.Thread, the name boost::unique_future was used for this class template (compare to boost::shared_future). Probably because of the standardization of std::future, more recent versions can use the name boost::future. Starting with version … Read more

How do I cleanly reconnect a boost::socket following a disconnect?

You need to create a new boost::asio::ip::tcp::socket each time you reconnect. The easiest way to do this is probably to just allocate the socket on the heap using a boost::shared_ptr (you could probably also get away with scoped_ptr if your socket is entirely encapsulated within a class). E.g.: bool MyClient::myconnect() { bool isConnected = false; … Read more

Is it okay to “Move” an object from a queue, if you’re about to pop from it?

Yes, this is perfectly safe: std::queue<T> q; // add stuff… T top = std::move(q.front()); q.pop(); pop() doesn’t have any preconditions on the first element in the q having a specified state, and since you’re not subsequently using q.front() you don’t have to deal with that object being invalidated any more. Sounds like a good idea … Read more

How to avoid memory leak with shared_ptr?

If you have circular references like this, one object should hold a weak_ptr to the other, not a shared_ptr. From the shared_ptr introduction: Because the implementation uses reference counting, cycles of shared_ptr instances will not be reclaimed. For example, if main() holds a shared_ptr to A, which directly or indirectly holds a shared_ptr back to … Read more

Boost 1.46.1, Property Tree: How to iterate through ptree receiving sub ptrees?

The property tree iterators point to pairs of the form (key, tree) of type ptree::value_type. The standard loop for iterating through the children of the node at path therefore looks like: BOOST_FOREACH(const ptree::value_type &v, pt.get_child(path)) { // v.first is the name of the child. // v.second is the child tree. }

What are the Best Components of Boost? [closed]

I use quite frequently (and it makes my life simpler): smart pointers (shared_ptr, scoped_ptr, weak_ptr, interprocess unique_ptr): scoped_ptr for basic RAII (without shared ownership and ownership transfer), at no cost. shared_ptr for more complex operations – when shared ownership is needed. However there is some cost. unique_ptr – there is active work at boost on … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)