Skip to content

Instantly share code, notes, and snippets.

@neworderofjamie
Created August 7, 2023 11:05
Show Gist options
  • Save neworderofjamie/35a5d4dc0ab47a225f1c14fea83aa097 to your computer and use it in GitHub Desktop.
Save neworderofjamie/35a5d4dc0ab47a225f1c14fea83aa097 to your computer and use it in GitHub Desktop.
First attempt at NumericTypeValue class
class NumericTypeValue
{
public:
NumericTypeValue(float value) : m_Value(double{value}){}
NumericTypeValue(double value) : m_Value(value){}
NumericTypeValue(int value): m_Value(int64_t{value}){}
NumericTypeValue(unsigned int value): m_Value(uint64_t{value}){}
NumericTypeValue(uint64_t value): m_Value(value){}
NumericTypeValue(int64_t value): m_Value(value){}
template<typename T>
T get() const{ return std::get<T>(m_Value); }
bool operator < (const NumericTypeValue &other)
{
return std::visit(
Overload{
[](uint64_t a, int64_t b) {
if(b < 0) {
return false;
}
else {
return (a < static_cast<uint64_t>(b));
}
},
[](int64_t a, uint64_t b) {
if(a < 0) {
return true;
}
else {
return (static_cast<uint64_t>(a) < b);
}
},
[](auto a, auto b) {
return (a < b);
}
},
m_Value, other.m_Value);
}
private:
std::variant<double, uint64_t, int64_t> m_Value;
};
int main()
{
NumericTypeValue minU = std::numeric_limits<uint64_t>::min();
NumericTypeValue maxU = std::numeric_limits<uint64_t>::max();
NumericTypeValue minS = std::numeric_limits<int64_t>::min();
NumericTypeValue maxS = std::numeric_limits<int64_t>::max();
NumericTypeValue minD = std::numeric_limits<double>::min();
NumericTypeValue maxD = std::numeric_limits<double>::max();
std::cout << (minS < minU) << std::endl;
std::cout << (maxD < maxU) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment