The best quantity creation helper #274
Replies: 5 comments 6 replies
-
Aliasesusing namespace units::aliases::isq;
si::length::km<> d(123);
si::speed::km_per_h<int> v(70); or using namespace units::aliases::isq;
si::length::km d(123.);
si::speed::km_per_h v(70); or using namespace units::aliases::isq::si;
auto d = km(123.);
auto v = km_per_h(70); |
Beta Was this translation helpful? Give feedback.
-
Referencesusing namespace units::isq::si::references;
auto d = 123. * km;
auto v = 70 * (km / h); |
Beta Was this translation helpful? Give feedback.
-
Literalsusing namespace units::isq::si::literals;
auto d = 123._q_km;
auto v = 70_q_km_per_h; |
Beta Was this translation helpful? Give feedback.
-
You say that all but one of theses features will be eventually removed. In the case of references, would it mean removing the |
Beta Was this translation helpful? Give feedback.
-
I'm new to using this library, but here are some of my initial thoughts on quantity creation. I'm not a fan of user-defined literals. You've put plenty of reasons why they aren't a good choice in the documentation, all of which I agree with. On the other hand, I think references are really useful and readable. To me, an expression like However, I'm not sure declaring a huge list of references as is currently implemented is the best way. As pointed out above, short variable names that can be imported into the global namespace aren't a good idea. Also, shortened names makes the link between the unit and its reference more confusing e.g.: it isn't immediately clear that Why not leave creating references entirely up to the user? It keeps the relationship to the corresponding quantity clear, the user can pick a shortened name that won't conflict, and they would have to do this anyway for any derived unit. Also pairs well with aliases:
I think aliases are a good idea as well, and they are useful beyond construction (as more readable class members and function arguments). However, for the same reasons as above, I'm not sure implementing a huge list of aliases is the best way. This is especially true for unit-specific aliases. I don't think I did have an idea for defining aliases while cutting down on extra code, but I have limited working of the internals of the library so please feel free to critique. Instead of defining a dimension (
TLDR: User-defined literals bad, references and aliases good. Leave actually creating references and aliases up to the user: it saves on a lot of code, and you'll never cover all the possibilities the user might need anyway. |
Beta Was this translation helpful? Give feedback.
-
Right now we have three different kinds of quantity creation helpers:
We should try to choose the best one that will remain in the library. All others will eventually be removed and only one will end up in the final product (C++ Standard???).
All of them are described in detail in the Quantity Construction Helpers documentation chapter.
To get more gut feeling of each option you can also compare the examples in the following three directories:
Please familiarize yourself with the documentation mentioned above and then vote, comment, or provide feedback on the below options.
Also, if I didn't account for some important feature in the documentation, please let me know in the comments below and I will update the docs.
Beta Was this translation helpful? Give feedback.
All reactions