Boost logo

Boost Users :

Subject: Re: [Boost-users] [Units] Temperature conversion problem
From: Alex Perry (Alex.Perry_at_[hidden])
Date: 2011-12-01 01:21:05


On Wed, 30 Nov 2011 15:00:28 -0800 Matthias Schabel <boost_at_[hidden]> wrote:
...snip...
> This will have the added benefit of allowing conversions but disallowing
> operations that don't make sense... Some code:
>
> #include <iostream>
>
> #include <boost/units/absolute.hpp>
> #include <boost/units/systems/temperature/celsius.hpp>
> #include <boost/units/systems/temperature/fahrenheit.hpp>
> #include <boost/units/systems/si/temperature.hpp>
>
> #include <boost/units/base_units/temperature/conversions.hpp>
>
> using namespace boost::units;
>
> int main()
> {
> quantity<celsius::temperature>
> dTC(10.*celsius::temperature());
> quantity<fahrenheit::temperature>
> dTF(10.*fahrenheit::temperature());
> quantity<si::temperature> dTK(10.*si::kelvin);
>
> std::cout << dTC << std::endl;
> std::cout << dTF << std::endl;
> std::cout << dTK << std::endl;
>
> quantity<absolute<celsius::temperature> >
> TC(0*absolute<celsius::temperature>());
> quantity<absolute<fahrenheit::temperature> >
> TF(32.*absolute<fahrenheit::temperature>());
> quantity<absolute<si::temperature> >
> TK(0.*absolute<si::temperature>());
>
> std::cout << TC << std::endl;
> std::cout << TF << std::endl;
> std::cout << TK << std::endl;
>
> std::cout << quantity<absolute<celsius::temperature> >(TF) <<
> std::endl;
> std::cout << quantity<absolute<celsius::temperature> >(TK) <<
> std::endl;
>
> std::cout << quantity<absolute<fahrenheit::temperature> >(TC) <<
> std::endl;
> std::cout << quantity<absolute<fahrenheit::temperature> >(TK) <<
> std::endl;
>
> // std::cout << TC+quantity<absolute<celsius::temperature> >(TF) <<
> std::endl; // fails because you cannot add two absolute temperatures
> std::cout << TC+quantity<celsius::temperature>(dTF) << std::endl;
> std::cout << TF+quantity<fahrenheit::temperature>(dTC) <<
> std::endl;
>
> return 0;
> }

Not being a current user of boost units I was curious about above code and just thought I'd try it (it looked totally straightforward and I was thinking hmm maybe I should be using boost units at times) - However it didn't compile (msvc 2010 and boost 1.48) - It wasn't exactly hard to change it so it would compile - just played around a little and ended up adding :-

#include <boost/units/quantity.hpp>
#include <boost/units/io.hpp>

However given your email had outputs pasted in so obviously code was running at some point I wondered : Was this just a cut & paste typo? Or do you need to be careful with headers in code using boost unit to make sure that it compiles x-platform or some such?

Alex


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net