Open danieltowner opened 1 year ago
I'd like to see a paper to SG6 proposing a general value_type
/ representation type cast.
std::value_cast<double>(1)
: int
to double
std::value_cast<int>(simd)
: simd<T>
to rebind_simd_t<int, simd<T>>
std::value_cast<float>(5min)
: duration<int, std::ratio<60>>
to duration<float, std::ratio<60>>
std::value_cast<double>(1.f + 1.if)
: complex<float> to complex<double>
@mpusz FYI. I guess such a cast would be interesting for mp-units as well. It would be equivalent to your quantity_cast
overload for casting the representation type.
Yes, we call it a representation_cast
in mp-units V2 framework for now but value_cast
is also a good name. However, in our case it will cast not only an underlying representation type but also will be used to change the unit as such conversion may also change/truncate the value. To make it even more entertaining, Unit is provided by value (NTTP) and representation type as a type parameter kind.
For example:
auto s1 = representation_cast<int>(speed);
auto s2 = representation_cast<km / h>(speed);
Assuming WG21 adds a std::value_cast
with a specific goal to allow UDTs to reuse it. Then there would be some mechanism to call a function defined by mp-units that implements the cast.
I wrote a quick prototype: https://godbolt.org/z/M6e5q9ao7.
Who wants to run with it and write a paper? :wink:
Real-world use of rebind_simd_t suggests that simd_cast is a concise and readable way of indiciating what you mean, while rebind_simd_t is a bit verbose.