template<typename _Rep, typename _Period = ratio<1>> struct duration;
typedef duration<int64_t> seconds;
So what I have come up with is a base structure defined as follow:template<typename _Rep, typename _Period, typename _Quantity> struct quantity_base;Which is then used as follow, to redefine the time types (for example and tests):struct unit_time;template<typename _Rep, typename _Period = std::ratio<1>>using duration = quantity_base<_Rep, _Period, unit_time>;typedef duration<int64_t, std::milli> milliseconds;typedef duration<int64_t> seconds;typedef duration<int64_t, std::ratio<60>> minutes;template<typename _ToQuantity, typename _Rep, typename _Period>constexpr typename __check_unit<unit_time, _ToQuantity>::typeduration_cast(const duration<_Rep, _Period>& __d){return quantity_base_cast<_ToQuantity>(__d);}
The rest of the templates are almost exactly the same as from chrono, but it takes into account the _Quantity template argument to ensure one can not mix different quantities;
Using this base implementation one can then add new types as follow, for example distance:
struct unit_distance;
template<typename _Rep, typename _Period = std::ratio<1>>
using distance = quantity_base<_Rep, _Period, unit_distance>;
template<typename _ToQuantity, typename _Rep, typename _Period>
constexpr typename __check_unit<unit_distance, _ToQuantity>::type
distance_cast(const distance<_Rep, _Period>& __d)
{
return quantity_base_cast<_ToQuantity>(__d);
}
typedef distance<int64_t, std::milli> millimeters;
typedef distance<int64_t> meters;
typedef distance<int64_t, std::kilo> kilometers;
This implementation is not nearly as advanced as some other libraries, notably the "Unit Conversion and Dimensional Analysis Library" from Nic Holthaus[1]
Is this something that could possibly be incorporated or is planned for future updates? Not really suggesting to use my implementation, but more the general idea to add support for more units and/or the ability for users to add other units, without having to code all the detail but reuse the "magic" already there in the chrono library.
[1] Unit Conversion and Dimensional Analysis Library
It's possible, as long as duration doesn't actually change its kind (it's a
struct today, it shall remain a struct).
| From: carel.c...@gmail.com Sent: Tuesday, May 30, 2017 2:58 PM To: ISO C++ Standard - Future Proposals Reply To: std-pr...@isocpp.org Subject: Re: [std-proposals] Why is chrono so shortsighted? |
It's possible, as long as duration doesn't actually change its kind (it's a
struct today, it shall remain a struct).
I hear talk of work on a std::units library.I think a group of people worked on it at Boostcon / C++Now
Your objective was to add more units, so I think you should investigate the
solution that includes more units before you investigate the implementation
details (what needs to change in chrono).
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1e74c14a-884d-4c4b-b000-a72a8d33e1ec%40isocpp.org.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/1e74c14a-884d-4c4b-b000-a72a8d33e1ec%40isocpp.org.
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAKiZDp0ms1%2BUJbEti9h2qWXkB%3DTB3xHeYTB8fP330fsiLknuyQ%40mail.gmail.com.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAKiZDp0ms1%2BUJbEti9h2qWXkB%3DTB3xHeYTB8fP330fsiLknuyQ%40mail.gmail.com.
Here's another unusual case. Consider 4x4 transformation matrices. A transformation matrix takes positions/vectors in one space and transforms them into another. So for unit analysis, you want to be able to tag positions/vectors with a particular space. When you try to transform them by a matrix, if they're not in the same space as the input of the matrix, then you get an error. And the result of the transformation is a vector in the output space of the matrix.
This also would need to handle transformation composition. If you have two matrices A and B, you can only compose B with A if the output space of A matches the input space of B. Inverting a matrix swaps the input and output spaces. And so forth.
I don't suppose someone has found a way to resolve something like that.