-
Notifications
You must be signed in to change notification settings - Fork 1.6k
<utility>
: Implement to_underlying()
#1828
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cd0bdeb
Implement to_underlying()
SuperWig 84e7ea4
tests pass
SuperWig 0006e34
Merge remote-tracking branch 'upstream/main' into to_underlying
SuperWig e59550b
Move __cpp_lib_to_underlying in yvals_core.h
SuperWig d1cbfcc
Correct order of feature test macro.
SuperWig 224b2a1
Get rid of struct and is_valid, and add another test case.
SuperWig 69a4f0a
[skip ci] Remove newline
SuperWig 3955f87
Merge remote-tracking branch 'upstream/main' into to_underlying
SuperWig fa25216
[skip ci] correct order
SuperWig 1fe5369
[skip ci] Correct revision number in comment.
SuperWig bd7e2ff
Update tests/std/tests/P1682R3_to_underlying/test.cpp
CaseyCarter 1e516fd
Adjust banner.
StephanTLavavej 34b2a65
Merge branch 'main' into to_underlying
StephanTLavavej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
RUNALL_INCLUDE ..\usual_latest_matrix.lst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright(c) Microsoft Corporation. | ||
// SPDX - License - Identifier : Apache - 2.0 WITH LLVM - exception | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#include <cassert> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
using namespace std; | ||
|
||
// TRANSITION, GH-602 | ||
// template <class T> | ||
// constexpr bool can_underlying = requires(T e) { to_underlying(e); }; | ||
|
||
template <class, class = void> | ||
constexpr bool can_underlying = false; | ||
template <class T> | ||
constexpr bool can_underlying<T, void_t<decltype(to_underlying(declval<T>()))>> = true; | ||
|
||
enum enum1 : char { a = '1' }; | ||
enum class enum2 : int { b = 2 }; | ||
enum class enum3 : long { c = 3 }; | ||
enum class enum4 { d = 4 }; | ||
|
||
SuperWig marked this conversation as resolved.
Show resolved
Hide resolved
|
||
struct not_an_enum {}; | ||
|
||
static_assert(can_underlying<enum1>); | ||
static_assert(can_underlying<enum2>); | ||
static_assert(can_underlying<enum3>); | ||
static_assert(can_underlying<enum4>); | ||
static_assert(!can_underlying<not_an_enum>); | ||
static_assert(!can_underlying<int>); | ||
|
||
static_assert(is_same_v<decltype(to_underlying(enum1::a)), char>); | ||
static_assert(is_same_v<decltype(to_underlying(enum2::b)), int>); | ||
static_assert(is_same_v<decltype(to_underlying(enum3::c)), long>); | ||
static_assert(is_same_v<decltype(to_underlying(enum4::d)), int>); | ||
|
||
static_assert(noexcept(to_underlying(enum1::a))); | ||
|
||
constexpr bool test() { | ||
assert(to_underlying(enum1::a) == '1'); | ||
assert(to_underlying(enum2::b) == 2); | ||
assert(to_underlying(enum3::c) == 3); | ||
assert(to_underlying(enum4::d) == 4); | ||
return true; | ||
} | ||
|
||
int main() { | ||
test(); | ||
static_assert(test()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.