P1682R3 to_underlying() For Enumerations (#1828)

Co-authored-by: Casey Carter <cartec69@gmail.com>
Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
This commit is contained in:
Daniel Marshall 2021-06-29 11:26:30 +01:00 коммит произвёл GitHub
Родитель 7ed04d977e
Коммит 3a279357e0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 79 добавлений и 0 удалений

Просмотреть файл

@ -763,6 +763,13 @@ _NODISCARD constexpr bool in_range(const _Ty _Value) noexcept {
}
#endif // _HAS_CXX20
#if _HAS_CXX23
template <class _Ty>
_NODISCARD constexpr underlying_type_t<_Ty> to_underlying(_Ty _Value) noexcept {
return static_cast<underlying_type_t<_Ty>>(_Value);
}
#endif // _HAS_CXX23
#if _HAS_TR1_NAMESPACE
namespace _DEPRECATE_TR1_NAMESPACE tr1 {
using _STD get;

Просмотреть файл

@ -259,6 +259,7 @@
// _HAS_CXX23 directly controls:
// P1048R1 is_scoped_enum
// P1679R3 contains() For basic_string/basic_string_view
// P1682R3 to_underlying() For Enumerations
// Parallel Algorithms Notes
// C++ allows an implementation to implement parallel algorithms as calls to the serial algorithms.
@ -1344,6 +1345,7 @@
#if _HAS_CXX23
#define __cpp_lib_is_scoped_enum 202011L
#define __cpp_lib_string_contains 202011L
#define __cpp_lib_to_underlying 202102L
#endif // _HAS_CXX23
// EXPERIMENTAL

Просмотреть файл

@ -419,6 +419,7 @@ tests\P1423R3_char8_t_remediation
tests\P1502R1_standard_library_header_units
tests\P1614R2_spaceship
tests\P1645R1_constexpr_numeric
tests\P1682R3_to_underlying
tests\VSO_0000000_allocator_propagation
tests\VSO_0000000_any_calling_conventions
tests\VSO_0000000_c_math_functions

Просмотреть файл

@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
RUNALL_INCLUDE ..\usual_latest_matrix.lst

Просмотреть файл

@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#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 };
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());
}

Просмотреть файл

@ -1563,6 +1563,20 @@ STATIC_ASSERT(__cpp_lib_to_chars == 201611L);
#endif
#endif
#if _HAS_CXX23
#ifndef __cpp_lib_to_underlying
#error __cpp_lib_to_underlying is not defined
#elif __cpp_lib_to_underlying != 202102L
#error __cpp_lib_to_underlying is not 202102L
#else
STATIC_ASSERT(__cpp_lib_to_underlying == 202102L);
#endif
#else
#ifdef __cpp_lib_to_underlying
#error __cpp_lib_to_underlying is defined
#endif
#endif
#ifndef __cpp_lib_transformation_trait_aliases
#error __cpp_lib_transformation_trait_aliases is not defined
#elif __cpp_lib_transformation_trait_aliases != 201304L