boost-format

A convenient logging statement for C++ using boost::format

点点圈 提交于 2019-12-13 01:44:08
问题 I want to design a logging function with the following characteristics: based on std::string rather than char* supports variable number of variables, like printf accepts as first parameter a severity level avoids formatting overhead when severity level is below logging level as simple as printf, or nearly so I'm inclined to use boost::format because of its automatic type conversion. But here are some problems I see: Its syntax is a little awkward: format("Mgr %s on pid %d is in state %s" %

VC++ 2012 and Boost incompatibility - `throw()` specifications in library headers

孤者浪人 提交于 2019-12-11 07:58:02
问题 I have a new project where I cannot use boost::format. I get a compiler error complaining that boost's override of a virtual function, ~basic_altstringbuf, lacks a "throw()". Even the most trivial attempt to use boost::format does that. I have other projects where it works fine. I have verified that the new project uses the same include-paths for boost, and for the VC++ includes. All the projects have "Enable C++ Exceptions" set to Yes. The only explanation I can come up with is that the

Formatting n significant digits in C++ without scientific notation

拈花ヽ惹草 提交于 2019-12-10 02:51:51
问题 I want to format a floating point value to n significant digits but never using scientific notation (even if it would be shorter). The format specification %f doesn't deal in significant digits, and %g will sometimes give me scientific notation (which is inappropriate for my use). I want values in the form "123", "12.3", "1.23" or "0.000000123" . Is there an elegant way to do this using C++ or boost ? 回答1: The best way I know (and use it in my own code) is #include <string> #include <math.h>

boost::format with variadic template arguments

假装没事ソ 提交于 2019-12-01 01:16:12
问题 Suppose I have a printf -like function (used for logging) utilizing perfect forwarding: template<typename... Arguments> void awesome_printf(std::string const& fmt, Arguments&&... args) { boost::format f(fmt); f % /* How to specify `args` here? */; BlackBoxLogFunction(boost::str(f).c_str()); } (I didn't compile this but my real function follows this guideline) How can I "unroll" the variadic argument into the boost::format variable f ? 回答1: As is usual with variadic templates, you can use

boost::format and custom printing a std containers

帅比萌擦擦* 提交于 2019-11-29 09:30:27
问题 I have a function in my namespace ns that helps me print STL containers. For example: template <typename T> std::ostream& operator<<(std::ostream& stream, const std::set<T>& set) { stream << "{"; bool first = true; for (const T& item : set) { if (!first) stream << ", "; else first = false; stream << item; } stream << "}"; return stream; } This works great for printing with operator << directly: std::set<std::string> x = { "1", "2", "3", "4" }; std::cout << x << std::endl; However, using boost