-
Previous to 2.0, it was easy to initialize quantities with values, and extract values (with I know I can use unit symbols in order to do create new quantities (e.g. I looked at this #454 and there's no explanation of how to get what I want as well. I should be able to do
or The above post seems to imply I'd be forced to use the unit symbols here? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
If you're OK with CTAD, you can use |
Beta Was this translation helpful? Give feedback.
-
Do you have an example use case for library code that you can't write? You say that it's infeasible for unit symbols specifically, but maybe we could figure out a different way to write it, with something other than symbols, that would be more feasible. Same question for use cases where we want to "just get the underlying number, regardless of units". I'm very skeptical of these in particular. One of the only ones I know is for printing quantities --- and even here, it's not correct unless we also handle the unit directly next to it (i.e., by printing a label). I think this will be a general property. |
Beta Was this translation helpful? Give feedback.
-
Hi @Cazadorro! Thanks for your feedback. We can't construct a quantity from a raw value anymore because of safety. See FAQ in our documentation for more details. You can also find the following chapters interesting: If I understand your problem correctly, you do not want to import all the symbols to your namespace like: namespace my {
using namespace mp_units::si::unit_symbols;
} as this would export those to your customers, who may again do: using namespace my; I wouldn't be so worried about it. First, it is considered a bad practice in C++. Second, you may do one of the alternatives:
namespace my {
using mp_units::si::unit_symbols::kHz;
}
namespace my::unit_symbols {
using namespace mp_units::si::unit_symbols;
}
quantity q1(42, mp_units::si::kilo<mp_units::si::hertz>);
quantity q2 = 42 * mp_units::si::kilo<mp_units::si::hertz>;
namespace my {
using mp_units::si;
} quantity q1(42, kilo<hertz>);
quantity q2 = 42 * kilo<hertz>; This actually will also allow the following: using namespace my::unit_symbols;
quantity q1(42, kHz);
quantity q2 = 42 * kHz;
Also, there is one thing that I would point out here. It seems that you do not want to expose units to your users. Does it mean that all the quantities you use in a project are of the same unit? In such a case you can provide a simple factory function for them: template<typename T>
quantity<mp_units::si::kilo<mp_units::si::hertz>, T> make_quantity(T value)
{
return value * mp_units::si::kilo<mp_units::si::hertz>;
} You can also provide a simple helper to return the underlying value: template<typename T>
const T& get_value(const quantity<mp_units::si::kilo<mp_units::si::hertz>, T>& q)
{
return q.numerical_value_ref_in(mp_units::si::kilo<mp_units::si::hertz>);
} I hope that one of the above would be an acceptable solution for you. |
Beta Was this translation helpful? Give feedback.
If you're OK with CTAD, you can use
quantity(1, kHz)
: https://godbolt.org/z/9qqnWhPTj.To get the value back, call
.numerical_value_in(kHz)
(see https://mpusz.github.io/mp-units/latest/users_guide/use_cases/working_with_legacy_interfaces/).