1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:07:34 +00:00

LibJS: Use a template for 'Value from integral number' constructors

This also allows constructing from other integral types like u64, which
would have been ambiguous before (at least on i686):

```
error: call of overloaded 'Value(u64&)' is ambiguous
note: candidate: 'JS::Value::Value(i32)'
  175 |     explicit Value(i32 value)
      |              ^~~~~
note: candidate: 'JS::Value::Value(unsigned int)'
  164 |     explicit Value(unsigned value)
      |              ^~~~~
note: candidate: 'JS::Value::Value(long unsigned int)'
  153 |     explicit Value(unsigned long value)
      |              ^~~~~
note: candidate: 'JS::Value::Value(double)'
  141 |     explicit Value(double value)
      |              ^~~~~
```
This commit is contained in:
Linus Groh 2022-07-03 16:55:32 +02:00
parent c860d8f5be
commit 1132151f3d

View file

@ -132,7 +132,7 @@ public:
}
template<typename T>
requires(SameAs<RemoveCVReference<T>, bool>) explicit Value(T value)
requires(IsSameIgnoringCV<T, bool>) explicit Value(T value)
: m_type(Type::Boolean)
{
m_value.as_bool = value;
@ -150,18 +150,12 @@ public:
}
}
explicit Value(unsigned long value)
{
if (value > NumericLimits<i32>::max()) {
m_value.as_double = static_cast<double>(value);
m_type = Type::Double;
} else {
m_value.as_i32 = static_cast<i32>(value);
m_type = Type::Int32;
}
}
explicit Value(unsigned value)
// NOTE: A couple of integral types are excluded here:
// - i32 has its own dedicated Value constructor
// - i64 cannot safely be cast to a double
// - bool isn't a number type and has its own dedicated Value constructor
template<typename T>
requires(IsIntegral<T> && !IsSameIgnoringCV<T, i32> && !IsSameIgnoringCV<T, i64> && !IsSameIgnoringCV<T, bool>) explicit Value(T value)
{
if (value > NumericLimits<i32>::max()) {
m_value.as_double = static_cast<double>(value);