boost::property_tree XML pretty printing

The solution was to add the trim_whitespace flag to the call to read_xml: #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/xml_parser.hpp> int main( void ) { // Create an empty property tree object using boost::property_tree::ptree; ptree pt; // reading file.xml read_xml(“file.xml”, pt, boost::property_tree::xml_parser::trim_whitespace ); // writing the unchanged ptree in file2.xml boost::property_tree::xml_writer_settings<char> settings(‘\t’, 1); write_xml(“file2.xml”, pt, std::locale(), settings); return … Read more

How to build boost Version 1.58.0 using Visual Studio 2015 (Enterprise)

Unfortunately Boost documentation is quite verbose because it tries to take care of all OS and environments. Also, it skips over some time saving details. Here’s quick steps specifically for VS2015 with Boost 1.61. First, let’s understand that Boost is huge library with lots of contributors. Consequently, all of the Boost code is divided in … Read more

How do I “normalize” a pathname using boost::filesystem?

Boost v1.48 and above You can use boost::filesystem::canonical: path canonical(const path& p, const path& base = current_path()); path canonical(const path& p, system::error_code& ec); path canonical(const path& p, const path& base, system::error_code& ec); http://www.boost.org/doc/libs/1_48_0/libs/filesystem/v3/doc/reference.html#canonical v1.48 and above also provide the boost::filesystem::read_symlink function for resolving symbolic links. Boost versions prior to v1.48 As mentioned in other answers, … Read more

Conversion from boost::shared_ptr to std::shared_ptr?

Based on janm’s response at first I did this: template<class T> std::shared_ptr<T> to_std(const boost::shared_ptr<T> &p) { return std::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } template<class T> boost::shared_ptr<T> to_boost(const std::shared_ptr<T> &p) { return boost::shared_ptr<T>(p.get(), [p](…) mutable { p.reset(); }); } But then I realized I could do this instead: namespace { template<class SharedPointer> struct Holder { … Read more

How to enable_shared_from_this of both parent and derived

The OP solution can be made more convenient by defining the following on the base class. protected: template <typename Derived> std::shared_ptr<Derived> shared_from_base() { return std::static_pointer_cast<Derived>(shared_from_this()); } This can be made more convenient by placing it in a base class (for reuse). #include <memory> template <class Base> class enable_shared_from_base : public std::enable_shared_from_this<Base> { protected: template <class … Read more

unique_ptr boost equivalent?

It’s not possible to create something like unique_ptr without C++0x (where it’s part of the standard library, and so Boost doesn’t need to provide it). Specifically without rvalue references, which are a feature in C++0x, a robust implementation of unique_ptr is impossible, with or without Boost. In C++03, there are a few possible alternatives, although … Read more

Serializing and deserializing JSON with Boost

Note that property_tree interprets the keys as paths, e.g. putting the pair “a.b”=”z” will create an {“a”:{“b”:”z”}} JSON, not an {“a.b”:”z”}. Otherwise, using property_tree is trivial. Here is a little example. #include <sstream> #include <map> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> using boost::property_tree::ptree; using boost::property_tree::read_json; using boost::property_tree::write_json; void example() { // Write json. ptree pt; pt.put (“foo”, … Read more

Difference between C++11 std::bind and boost::bind

boost::bind has overloaded relational operators, std::bind does not. boost::bind supports non-default calling conventions, std::bind is not guaranteed to (standard library implementations may offer this as an extension). boost::bind provides a direct mechanism to allow one to prevent eager evaluation of nested bind expressions (boost::protect), std::bind does not. (That said, one can use boost::protect with std::bind … Read more