Skip to content

Commit

Permalink
Implement unreachable (#69)
Browse files Browse the repository at this point in the history
* Implement unreachable

* Fix test
  • Loading branch information
lackhole authored Jan 7, 2025
1 parent c504437 commit ce37198
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 13 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Implementation available in C++14 ~ :

| Header | Total | | C++17 | C++20 | C++23 | C++26 |
|-------------------------------------|---------------------------------------------------------|---|--------------------------------------------------------|---------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------|
| | ![](https://img.shields.io/badge/314/694-grey)![][p045] | | ![](https://img.shields.io/badge/44/113-grey)![][p039] | ![](https://img.shields.io/badge/233/429-grey)![][p054] | ![](https://img.shields.io/badge/59/144-grey)![][p041] | ![](https://img.shields.io/badge/18/135-grey)![][p013] |
| | ![](https://img.shields.io/badge/315/694-grey)![][p045] | | ![](https://img.shields.io/badge/44/113-grey)![][p039] | ![](https://img.shields.io/badge/233/429-grey)![][p054] | ![](https://img.shields.io/badge/60/144-grey)![][p042] | ![](https://img.shields.io/badge/18/135-grey)![][p013] |
| | | | | | | |
| [algorithm](#algorithm) | ![](https://img.shields.io/badge/55/115-grey)![][p048] | | ![](https://img.shields.io/badge/2/4-grey)![][p050] | ![](https://img.shields.io/badge/47/96-grey)![][p049] | ![](https://img.shields.io/badge/8/18-grey)![][p044] | ![](https://img.shields.io/badge/7/23-grey)![][p030] |
| [any](#any) | ![](https://img.shields.io/badge/5/5-grey)![][p100] | | ![](https://img.shields.io/badge/5/5-grey)![][p100] | | | |
Expand All @@ -129,7 +129,7 @@ Implementation available in C++14 ~ :
| [string_view](#string_view) | ![](https://img.shields.io/badge/7/7-grey)![][p100] | | ![](https://img.shields.io/badge/4/4-grey)![][p100] | ![](https://img.shields.io/badge/1/1-grey)![][p100] | ![](https://img.shields.io/badge/1/1-grey)![][p100] | ![](https://img.shields.io/badge/3/3-grey)![][p100] * |
| [tuple](#tuple) | ![](https://img.shields.io/badge/7/8-grey)![][p088] | | ![](https://img.shields.io/badge/2/2-grey)![][p100] | | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/5/6-grey)![][p083] |
| [type_traits](#type_traits) | ![](https://img.shields.io/badge/17/26-grey)![][p065] | | ![](https://img.shields.io/badge/8/10-grey)![][p080] | ![](https://img.shields.io/badge/7/13-grey)![][p054] | ![](https://img.shields.io/badge/2/6-grey)![][p033] | ![](https://img.shields.io/badge/0/2-grey)![][p000] |
| [utility](#utility) | ![](https://img.shields.io/badge/7/8-grey)![][p088] | | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/2/3-grey)![][p067] | ![](https://img.shields.io/badge/1/1-grey)![][p100] |
| [utility](#utility) | ![](https://img.shields.io/badge/8/8-grey)![][p100] | | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/2/2-grey)![][p100] | ![](https://img.shields.io/badge/3/3-grey)![][p100] | ![](https://img.shields.io/badge/1/1-grey)![][p100] |
| [variant](#variant) | ![](https://img.shields.io/badge/9/9-grey)![][p100] | | ![](https://img.shields.io/badge/9/9-grey)![][p100] | | | ![](https://img.shields.io/badge/1/1-grey)![][p100] |
| | | | | | | |
| [atomic](#atomic) | ![](https://img.shields.io/badge/0/17-grey)![][p000] | | | ![](https://img.shields.io/badge/0/13-grey)![][p000] | | ![](https://img.shields.io/badge/0/4-grey)![][p000] |
Expand Down Expand Up @@ -1230,16 +1230,16 @@ Description

#### `<utility>`

| | Introduced | Revision |
|-----------------------------|----------------|----------|
| `as_const` | ![][c17ok] | |
| `in_place`<br/>`in_place_t` | ![][c17ok] | |
| `cmp_equal`... | ![][c20ok] | |
| `in_range` | ![][c20ok] | |
| `forward_like` | ![][c23ok] | |
| `to_underlying` | ![][c23ok] | |
| `unreachable` | ![][c23no] N/A | |
| `nontype`<br/>`nontype_t` | ![][c26ok] | |
| | Introduced | Revision |
|-----------------------------|------------|----------|
| `as_const` | ![][c17ok] | |
| `in_place`<br/>`in_place_t` | ![][c17ok] | |
| `cmp_equal`... | ![][c20ok] | |
| `in_range` | ![][c20ok] | |
| `forward_like` | ![][c23ok] | |
| `to_underlying` | ![][c23ok] | |
| `unreachable` | ![][c23ok] | |
| `nontype`<br/>`nontype_t` | ![][c26ok] | |

#### `<variant>`

Expand Down
10 changes: 10 additions & 0 deletions include/preview/__core/noreturn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by yonggyulee on 06/01/2025
//

#ifndef PREVIEW_CORE_NORETURN_H_
#define PREVIEW_CORE_NORETURN_H_

#define PREVIEW_NORETURN [[noreturn]]

#endif // PREVIEW_CORE_NORETURN_H_
65 changes: 65 additions & 0 deletions include/preview/__utility/unreachable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Created by yonggyulee on 18/11/2024
//

#ifndef PREVIEW_INCLUDE_PREVIEW_UTILITY_UNREACHABLE_H_
#define PREVIEW_INCLUDE_PREVIEW_UTILITY_UNREACHABLE_H_

#include "preview/__core/std_version.h"

#if PREVIEW_CXX_VERSION >= 23

#include <utility>

namespace preview {
using std::unreachable;
} // namespace preview

#else

#include "preview/__core/noreturn.h"

# if defined(_MSC_VER)

namespace preview {
PREVIEW_NORETURN inline void unreachable() {
__assume(0);
}
} // namespace preview

# else

#include <cstdlib>
#include <type_traits>

#include "preview/__type_traits/void_t.h"


namespace preview {
namespace detail_unreachable {

template<typename = void>
struct adl_unreachable : std::false_type {};
template<>
struct adl_unreachable<void_t<decltype(__builtin_unreachable())>> : std::true_type {};

PREVIEW_NORETURN [[maybe_unused]] inline void unreachable_impl(std::true_type) {
__builtin_unreachable();
}

PREVIEW_NORETURN [[maybe_unused]] inline void unreachable_impl(std::false_type) {
abort();
}

} // namespace detail_unreachable

PREVIEW_NORETURN inline void unreachable() {
preview::detail_unreachable::unreachable_impl(detail_unreachable::adl_unreachable<>{});
}

} // namespace preview

# endif // defined(_MSC_VER)
#endif // PREVIEW_CXX_VERSION >= 23

#endif // PREVIEW_INCLUDE_PREVIEW_UTILITY_UNREACHABLE_H_
1 change: 1 addition & 0 deletions include/preview/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "preview/__core/inline_variable.h"
#include "preview/__core/no_unique_address.h"
#include "preview/__core/nodiscard.h"
#include "preview/__core/noreturn.h"
#include "preview/__core/std_version.h"

#endif // PREVIEW_CORE_H_
1 change: 1 addition & 0 deletions include/preview/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
#include "preview/__utility/to_underlying.h"
#include "preview/__utility/to_unsigned_like.h"
#include "preview/__utility/type_sequence.h"
#include "preview/__utility/unreachable.h"

#endif // PREVIEW_UTILITY_H_
1 change: 0 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ function(preview_add_multi_version_test name_prefix test_src)
target_compile_options(${target_name}
PUBLIC "-fexceptions"
)
gtest_discover_tests(${target_name} DISCOVERY_TIMEOUT 60)
elseif (MSVC)
target_compile_options(${target_name}
PRIVATE "/bigobj"
Expand Down
8 changes: 8 additions & 0 deletions test/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ TEST(VERSIONED(Utility), force_forward_like) {
EXPECT_EQ(preview::force_forward_like<decltype((my_state))>(cref_my_state.from_opt())(), kTypeLvalueReference);
EXPECT_EQ(preview::force_forward_like<decltype((my_state))>(cref_my_state[0])(), kTypeLvalueReference);
}

#ifdef GTEST_HAS_DEATH_TEST
TEST(VERSIONED(Utility), unreachable) {
//#ifndef NDEBUG
// EXPECT_DEATH(preview::unreachable(), "");
//#endif
}
#endif

0 comments on commit ce37198

Please sign in to comment.