-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Poll: How to constrain operations in a class template? #29
Comments
@mpusz, here you have pointed out that
Option 1 will not allow it, because division is not defined (mathematically speaking) for vectors. This is the first reason, why I would prefer Option 2. The second reason to prefer Option 2 is that it can potentially provide a way to have non-additive quantities and stuff like that. E.g. it would make sense to prohibit adding absolute temperatures to one another using a restrictive |
@i-ky Thanks for your feedback! Regarding "second reason" we will probably solve the temperature problem with a dedicated affine space support. There will be no need to restrict |
I guess by "temperature problem" you mean conversions between unit that have different zeros. I had a different thing in mind. Imagine we have two temperatures (could be air temperatures in different places or on different days). What would adding them up mean? Does the sum of two temperatures have physical meaning? I think no. However, it makes sense to multiply temperature by Bolzmann constant and number of particles to get heat energy, which can be added together. Or you can multiply temperature by duration of time during which it was constant, add these numbers (in Your library provides conversions between different units. Maybe someone will be able to come up with a clever fool-proof type system for thermodynamics or other domains. |
Even more "illogically", it seems that it's sometimes appropriate to "subtract" temperatures: eg Spec Heat Capacity = m * C * dt I think these "illogical" exceptions are probably not the concern of a unit library? |
The current approach seems like the right one. By constraining an operator on the representation's operator, |
Perhaps one can fit dimensional analysis into those like this: export template <class T>
concept quantity_field = std::regular<T> && requires(const T& c, T& l) {
// zero<T>, one<T>, +c, -c, l+=c, l-=c, c+c, c-c
{ c * c } -> std::regular;
{ c / c } -> std::regular;
{ c * c / c } -> std::common_with<T>;
};
export template <class T>
concept field = quantity_field<T> && requires(const T& c, T& l) {
{ l *= c } -> std::same_as<T&>;
{ l /= c } -> std::same_as<T&>;
{ c * c } -> std::common_with<T>;
{ c / c } -> std::common_with<T>;
}; |
My first try was to reuse http://wg21.link/P01813 but those concepts were overconstrained for our needs. After that, I went with It does not mean the |
Things have changed since. I have been working on my own number concepts for quite some time now. I recently improved naming based on hsutter/cppfront#231 (comment) In case you're interested in how I constrained quantities before,
There might be room for improving this hierarchy of number concepts:
Anyways, I think using these concepts is an improvement over the status-quo. |
This looks super interesting! 😃 |
The more I think about the "number" concept (#28) the more doubts I have. I see 2 solutions here:
Option 1
In this case,
Rep
has to satisfy all of the requirements of theNumber
concepts. It means that even if the user is never going to useoperator %=
on aquantity
his/her representation type has to provide it or thequantity
class template instantiation will fail.Option 2
In this case, the user will be able to instantiate the
quantity
class template for every "sane" type and will be able to use only those arithmetic operations that are supported by the underlyingRep
type.Which option do you prefer?
(please vote only once by clicking on the correct bar above)
The text was updated successfully, but these errors were encountered: