-
Notifications
You must be signed in to change notification settings - Fork 38
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
capi: Add static assertions for FizzyValueType* vs. fizzy:ValType::* #821
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Fizzy: A fast WebAssembly interpreter | ||
// Copyright 2022 The Fizzy Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#pragma once | ||
|
||
#include <type_traits> | ||
|
||
namespace fizzy | ||
{ | ||
/// C++14 version of the proposed C++23 feature | ||
/// https://en.cppreference.com/w/cpp/utility/to_underlying | ||
template <typename T> | ||
constexpr std::underlying_type_t<T> to_underlying(T value) noexcept | ||
{ | ||
return static_cast<std::underlying_type_t<T>>(value); | ||
} | ||
} // namespace fizzy |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Fizzy: A fast WebAssembly interpreter | ||
// Copyright 2022 The Fizzy Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include "cxx23/utility.hpp" | ||
#include "stack.hpp" | ||
#include <gtest/gtest.h> | ||
#include <array> | ||
#include <vector> | ||
|
||
using namespace fizzy; | ||
using namespace testing; | ||
|
||
TEST(cxx23_utility, to_underlying) | ||
{ | ||
enum class A | ||
{ | ||
A = 0xff | ||
}; | ||
static_assert(std::is_same_v<decltype(to_underlying(A::A)), int>); | ||
EXPECT_EQ(to_underlying(A::A), 0xff); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add `static_assert(std::is_same_v<decltype(to_underlying(A::A)), int>); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specs said it is at least There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also did you know that the C enum ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be bigger than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For scoped enumerations ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like we are adding C++ compiler compliance tests at this point 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just wanted to know if this returns the same type as in the enum declaration. If you want to be explicit, you can define it as |
||
|
||
enum class A8 : uint8_t | ||
{ | ||
A = 0xff | ||
}; | ||
static_assert(std::is_same_v<decltype(to_underlying(A8::A)), uint8_t>); | ||
EXPECT_EQ(to_underlying(A8::A), 0xff); | ||
|
||
enum class A32 : uint32_t | ||
{ | ||
A = 0xff, | ||
B = 0xffffffff | ||
}; | ||
static_assert(std::is_same_v<decltype(to_underlying(A32::A)), uint32_t>); | ||
EXPECT_EQ(to_underlying(A32::A), 0xff); | ||
EXPECT_EQ(to_underlying(A32::B), 0xffffffff); | ||
|
||
enum C | ||
{ | ||
A = 0xff | ||
}; | ||
EXPECT_EQ(to_underlying(C::A), 0xff); | ||
} | ||
axic marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this overkill?