[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

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
#include #include #include #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 quantitysi::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 quanttyus::foot_base_unit::unit_type Length; Length x(2.0 * m); In Christ, Steven Watanabe

(See reply at bottom)
----- Original Message -----
From: "Steven Watanabe"
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 quanttyus::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 quantitysi::length Length; Length x = 3.0 * ft; cout << "x=" << convert_to_ft(x) << endl; terry

AMDG Terry Golubiewski wrote:
Warning: untested code
typedef quanttyus::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 quantitysi::length Length; Length x = 3.0 * ft; cout << "x=" << convert_to_ft(x) << endl;
typedef quantityus::foot_base_unit::unit_type convert_to_ft; In Christ, Steven Watanabe

----- Original Message -----
From: "Steven Watanabe"
AMDG
Terry Golubiewski wrote:
Warning: untested code
typedef quanttyus::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 quantitysi::length Length; Length x = 3.0 * ft; cout << "x=" << convert_to_ft(x) << endl;
typedef quantityus::foot_base_unit::unit_type convert_to_ft;
Got it! Now the other part... typedef quantitysi::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 quantitysi::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 quantitysi::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
, 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"
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