Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
7 changes: 7 additions & 0 deletions stl/inc/utility
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions stl/inc/yvals_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@

// _HAS_CXX23 directly controls:
// P1048R1 is_scoped_enum
// P1682R2 to_underlying() For Enumerations

// Parallel Algorithms Notes
// C++ allows an implementation to implement parallel algorithms as calls to the serial algorithms.
Expand Down Expand Up @@ -1340,6 +1341,7 @@

// C++23
#if _HAS_CXX23
#define __cpp_lib_to_underlying 202102L
#define __cpp_lib_is_scoped_enum 202011L
#endif // _HAS_CXX23

Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/P1682R3_to_underlying/env.lst
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
51 changes: 51 additions & 0 deletions tests/std/tests/P1682R3_to_underlying/test.cpp
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

#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());
}
Original file line number Diff line number Diff line change
Expand Up @@ -1549,6 +1549,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
Expand Down