Basically, I'd like to implement a float16
type. But this question is not about the details of how to do that, but instead how to set things up so that my new float16 type behaves appropriately with float, double, and all the integer types.
What I would like is for my float16
type to convert analogously as to float or double. For instance, it should implicitly cast to both of these types. It should also have std::common_type (http://en.cppreference.com/w/cpp/types/common_type) behaving analogously for it as it std::common_types behaves for the other float types. This means that std::common_type<my_float16, float>::type = float
, std::common_type<my_float16, double>::type = double
, and std::common_type<my_float16, T>::type = my_float16
where T
is any integer type.
What constructors and cast operators do I need to write to make this work? Any help would be appreciated!
My other recent question might be related.
EDIT: Okay, I've built a minimal example like Anton did. It is
struct float16 {
explicit operator int() {
return 0;
}
operator float() {
return 0.0f;
}
};
This has the right common type for float and float16, but the common type for int and float16 is still int. I do not understand that.