Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct Value only from uint32_t/uint64_t #746

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/fizzy/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ union Value

/// Converting constructors from integer types
///
/// We need to support {signed,unsigned} x {32,64} integers. However, due to uint64_t being
/// defined differently in different implementations we need to avoid the alias and provide
/// constructors for unsigned long and unsigned long long independently.
constexpr Value(unsigned int v) noexcept : i32{v} {}
constexpr Value(unsigned long v) noexcept : i64{v} {}
constexpr Value(unsigned long long v) noexcept : i64{v} {}
constexpr Value(int64_t v) noexcept : i64{static_cast<uint64_t>(v)} {}
/// Only fixed-size integer types are supported: {signed,unsigned} x {32,64}.
/// On the platforms where uint64_t is not unsigned long, it is expected that
/// Value cannot be constructed out of unsigned long literals.
constexpr Value(uint32_t v) noexcept : i32{v} {}
constexpr Value(uint64_t v) noexcept : i64{v} {}
constexpr Value(int32_t v) noexcept : i32{static_cast<uint32_t>(v)} {}
constexpr Value(int64_t v) noexcept : i64{static_cast<uint64_t>(v)} {}

constexpr Value(float v) noexcept : f32{v} {}
constexpr Value(double v) noexcept : f64{v} {}
Expand Down
4 changes: 1 addition & 3 deletions test/unittests/value_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ TEST(value, constructor_from_unsigned_ints)
{
EXPECT_EQ(Value{uint32_t{0xdededefe}}.i32, 0xdededefe);
EXPECT_EQ(Value{uint64_t{0xdededededededefe}}.i64, 0xdededededededefe);
EXPECT_EQ(Value{static_cast<unsigned long>(0xdededededededefe)}.i64, 0xdededededededefe);
EXPECT_EQ(Value{static_cast<unsigned long long>(0xdededededededefe)}.i64, 0xdededededededefe);
}

TEST(value, constructor_from_signed_ints)
Expand All @@ -88,7 +86,7 @@ TEST(value, as_integer_32bit_value)

TEST(value, as_integer_64bit_value)
{
const Value v{0xfffffffffffffffe};
const Value v{uint64_t{0xfffffffffffffffe}};
EXPECT_EQ(v.as<uint64_t>(), 0xfffffffffffffffe);
EXPECT_EQ(v.as<int64_t>(), -2);
}
Expand Down