From 1132151f3df3a703fd341125c9b1dccd367ab057 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 3 Jul 2022 16:55:32 +0200 Subject: [PATCH] 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) | ^~~~~ ``` --- Userland/Libraries/LibJS/Runtime/Value.h | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h index 4eb3d39558..6bcbdeb6ac 100644 --- a/Userland/Libraries/LibJS/Runtime/Value.h +++ b/Userland/Libraries/LibJS/Runtime/Value.h @@ -132,7 +132,7 @@ public: } template - requires(SameAs, bool>) explicit Value(T value) + requires(IsSameIgnoringCV) 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::max()) { - m_value.as_double = static_cast(value); - m_type = Type::Double; - } else { - m_value.as_i32 = static_cast(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 + requires(IsIntegral && !IsSameIgnoringCV && !IsSameIgnoringCV && !IsSameIgnoringCV) explicit Value(T value) { if (value > NumericLimits::max()) { m_value.as_double = static_cast(value);