[units] How to assign "ft" to an si::length?
I am having trouble seeing how to use mixed units. The program below compiles and runs. See the changes I "wish" I could make as comments. terry #include <boost/units/systems/si.hpp> #include <boost/units/base_units/us/foot.hpp> #include <boost/units/quantity.hpp> #include <boost/units/io.hpp> #include <iostream> #include <iomanip> using namespace std; using namespace boost; using namespace boost::units; int main() { static const si::length m = si::meter; // static const si::length ft = ???; typedef quantity<si::length> Length; Length x = 2.0 * m; Length y = 3.0 * m; // instead of "m" use "ft"; cout << "x=" << x << " y=" << y << endl; Length z = x + y; cout << "x+y=" << z << endl; // display result in "ft" return 0; } // main
AMDG Terry Golubiewski wrote:
I am having trouble seeing how to use mixed units. The program below compiles and runs. See the changes I "wish" I could make as comments.
terry
#include <boost/units/systems/si.hpp> #include <boost/units/base_units/us/foot.hpp> #include <boost/units/quantity.hpp> #include <boost/units/io.hpp>
#include <iostream> #include <iomanip>
using namespace std; using namespace boost; using namespace boost::units;
int main() { static const si::length m = si::meter; // static const si::length ft = ???; typedef quantity<si::length> Length; Length x = 2.0 * m; Length y = 3.0 * m; // instead of "m" use "ft"; cout << "x=" << x << " y=" << y << endl; Length z = x + y; cout << "x+y=" << z << endl; // display result in "ft" return 0; } // main
Warning: untested code typedef quantty<us::foot_base_unit::unit_type> Length; Length x(2.0 * m); In Christ, Steven Watanabe
(See reply at bottom) ----- Original Message ----- From: "Steven Watanabe" <watanabesj@gmail.com> Newsgroups: gmane.comp.lib.boost.user To: <boost-users@lists.boost.org> Sent: Wednesday, May 19, 2010 1:17 PM Subject: Re: [units] How to assign "ft" to an si::length?
AMDG
Terry Golubiewski wrote:
I am having trouble seeing how to use mixed units. The program below compiles and runs. See the changes I "wish" I could make as comments.
terry
<snip>
Warning: untested code
typedef quantty<us::foot_base_unit::unit_type> Length; Length x(2.0 * m);
In Christ, Steven Watanabe
Ok. That makes Length represent "feet" But I want Length to remain represented as an si::length, but input "ft" into it and convert to "ft" to print. // static const si::length ft = ???; typedef quantity<si::length> Length; Length x = 3.0 * ft; cout << "x=" << convert_to_ft(x) << endl; terry
AMDG Terry Golubiewski wrote:
Warning: untested code
typedef quantty<us::foot_base_unit::unit_type> Length; Length x(2.0 * m);
Ok. That makes Length represent "feet" But I want Length to remain represented as an si::length, but input "ft" into it and convert to "ft" to print.
// static const si::length ft = ???; typedef quantity<si::length> Length; Length x = 3.0 * ft; cout << "x=" << convert_to_ft(x) << endl;
typedef quantity<us::foot_base_unit::unit_type> convert_to_ft; In Christ, Steven Watanabe
----- Original Message ----- From: "Steven Watanabe" <watanabesj@gmail.com> Newsgroups: gmane.comp.lib.boost.user To: <boost-users@lists.boost.org> Sent: Wednesday, May 19, 2010 2:15 PM Subject: Re: [units] How to assign "ft" to an si::length?
AMDG
Terry Golubiewski wrote:
Warning: untested code
typedef quantty<us::foot_base_unit::unit_type> Length; Length x(2.0 * m);
Ok. That makes Length represent "feet" But I want Length to remain represented as an si::length, but input "ft" into it and convert to "ft" to print.
// static const si::length ft = ???; typedef quantity<si::length> Length; Length x = 3.0 * ft; cout << "x=" << convert_to_ft(x) << endl;
typedef quantity<us::foot_base_unit::unit_type> convert_to_ft;
Got it! Now the other part... typedef quantity<si::length> Length; Length x = 3.0 * ft; If x is an SI unit, how can I convert from "ft"? I'm missing something, but I'm not sure what. terry
AMDG Terry Golubiewski wrote:
Got it! Now the other part...
typedef quantity<si::length> Length; Length x = 3.0 * ft;
If x is an SI unit, how can I convert from "ft"? I'm missing something, but I'm not sure what.
First define ft as static const us::foot_base_unit::unit_type ft; Then Length x(3.0 * ft); should work. In Christ, Steven Watanabe
So far, so good. Thank you very much for your patience. This last one should carry me until tomorrow... typedef quantity<si::length> Length; typedef [what goes here?] km_unit_type; const km_unit_type = km; Length x = 3.0 * km; terry
Length x = 3.0 ft did not compile, see below... terry #include <boost/units/systems/si.hpp> #include <boost/units/base_units/us/foot.hpp> #include <boost/units/quantity.hpp> #include <boost/units/io.hpp> #include <iostream> #include <iomanip> using namespace std; using namespace boost; using namespace boost::units; int main() { typedef quantity<si::length> Length; static const si::length m = si::meter; Length x = 2.0 * m; // This works! cout << x << endl; typedef us::foot_base_unit::unit_type foot_unit; static const foot_unit ft; Length y = 3.0 * ft; // This does not compile :o( cout << y << endl; return 0; } // main // Compiler output... tjg.cpp using native typeof tjg.cpp(21) : error C2440: 'initializing' : cannot convert from 'units::quantity<Unit, Y>' to 'units::quantity<Unit>' with [ Unit=units::unit< units::list< units::dim<units::length_base_dimension, units::static_rational<1>>, units::dimensionless_type >, units::heterogeneous_system< units::heterogeneous_system_impl< units::list< units::heterogeneous_system_dim< units::scaled_base_unit< units::us::yard_base_unit, units::scale<3, units::static_rational<-1>> >, units::static_rational<1> >, units::dimensionless_type >, units::list< units::dim<units::length_base_dimension, units::static_rational<1>>, units::dimensionless_type >, units::dimensionless_type > >
, Y=double ] and [ Unit=units::si::length ] Constructor for class 'units::quantity<Unit>' is declared 'explicit' with [ Unit=units::si::length ]
Yep, that's it. Sorry. I thought they were equivalent. Hmm. Could you please point me to somewhere that explains the difference between implicit and explicit construction? terry ----- Original Message ----- From: "Matthias Schabel" <boost@schabel-family.org> Newsgroups: gmane.comp.lib.boost.user To: <boost-users@lists.boost.org> Sent: Wednesday, May 19, 2010 7:31 PM Subject: Re: [units] How to assign "ft" to an si::length?
Explicit construction works, but assignment (which is implicit) does not...that's why Steven keeps recommending the explicit constructor - XX(3.0*ft) - syntax.
Matthias
Length x = 3.0 ft did not compile, see below...
AMDG Terry Golubiewski wrote:
Yep, that's it. Sorry. I thought they were equivalent. Hmm. Could you please point me to somewhere that explains the difference between implicit and explicit construction?
X x = y; is defined to call the copy constructor of X. This requires an implicit conversion from y to X. X x(y); does normal overload resolution for the constructor. In Christ, Steven Watanabe
participants (3)
-
Matthias Schabel
-
Steven Watanabe
-
Terry Golubiewski