[units] - overriding output

What is the correct way to override the << op for a unit so that it say prints "Pa" instead of "m^-1 kg s^-2"? I tried this and no worky: #include <boost/units/io.hpp> namespace boost { namespace units { template<> inline std::string name_string< boost::units::pressure_dimension, boost::units::si::system
(const boost::units::si::pressure&) { return "Pa"; }
}} // boost::units Also, I note that the operator << in io.hpp has a comment saying "INTERNAL USE ONLY". How would you suggest then that users of the library provide for stream output without running afoul of this implementation we're not meant to use?

What is the correct way to override the << op for a unit so that it say prints "Pa" instead of "m^-1 kg s^-2"?
I tried this and no worky:
#include <boost/units/io.hpp>
namespace boost { namespace units {
template<> inline std::string name_string< boost::units::pressure_dimension, boost::units::si::system >(const boost::units::si::pressure&) { return "Pa"; }
}} // boost::units
"name_string" and "symbol_string" aren't templated - they are overloaded functions, so the template is not correct. Also, you defined "name_string", which is for fully-formatted unit output (e.g. "pascal" vs. "Pa"), so you are overloading the wrong function here. The easy way is to include the SI IO header : #include <boost/units/io.hpp> #include <boost/units/quantity.hpp> #include <boost/units/systems/si.hpp> #include <boost/units/systems/si/io.hpp> #include <iostream> using namespace boost::units; int main(void) { std::cout << name_format << 1.0*si::pascal << std::endl << symbol_format << 1.0*si::pascal << std::endl; } giving 1 pascal 1 Pa The default output formatter is symbol format. Your code, with fixes of the bugs above, works as well : #include <boost/units/io.hpp> #include <boost/units/systems/si.hpp> namespace boost { namespace units { inline std::string symbol_string(const si::pressure&) { return "Pa"; } } } // boost::units #include <iostream> using namespace boost::units; int main(void) { std::cout << 1.0*si::pascal << std::endl; } HTH, Matthias

Matthias Schabel wrote:
What is the correct way to override the << op for a unit so that it say prints "Pa" instead of "m^-1 kg s^-2"?
I tried this and no worky:
#include <boost/units/io.hpp>
namespace boost { namespace units {
template<> inline std::string name_string< boost::units::pressure_dimension, boost::units::si::system >(const boost::units::si::pressure&) { return "Pa"; }
}} // boost::units
"name_string" and "symbol_string" aren't templated - they are overloaded functions, so the template is not correct. Also, you defined "name_string", which is for fully-formatted unit output (e.g. "pascal" vs. "Pa"), so you are overloading the wrong function here. The easy way is to include the SI IO header :
#include <boost/units/io.hpp> #include <boost/units/quantity.hpp> #include <boost/units/systems/si.hpp> #include <boost/units/systems/si/io.hpp>
Well, thanks. Unfortunately although this works for every test program I can imagine that looks like my production code....it just plain doesn't in the actual code I'm trying to develop. No matter what I do, it always prints out m^-1 kg s^-2 for the unit and no matter how I wrap up typedefs and usings inside my own namespace I can't reproduce the problem in simple code. Some demon from the deepest pit of hell is keeping the overload resolution from working only in production code.
participants (2)
-
Matthias Schabel
-
Noah Roberts