Skip to content

Commit a46f0c9

Browse files
committed
Add checked_bit_cast
1 parent b79ccf7 commit a46f0c9

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

source/bounded/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ target_sources(bounded PUBLIC
5151
cast.cpp
5252
character.cpp
5353
check_in_range.cpp
54+
checked_bit_cast.cpp
5455
clamp.cpp
5556
common_type_and_value_category.cpp
5657
comparison.cpp

source/bounded/bounded.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export import bounded.builtin_min_max_value;
1515
export import bounded.cast;
1616
export import bounded.character;
1717
export import bounded.check_in_range;
18+
export import bounded.checked_bit_cast;
1819
export import bounded.clamp;
1920
export import bounded.common_type_and_value_category;
2021
export import bounded.comparison;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright David Stone 2024.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// (See accompanying file LICENSE_1_0.txt or copy at
4+
// http://www.boost.org/LICENSE_1_0.txt)
5+
6+
export module bounded.checked_bit_cast;
7+
8+
import bounded.bounded_integer;
9+
import bounded.check_in_range;
10+
11+
import std_module;
12+
13+
namespace bounded {
14+
15+
export template<bounded_integer T, typename Source>
16+
constexpr auto checked_bit_cast(Source const source) -> T {
17+
return bounded::check_in_range<T>(std::bit_cast<typename T::underlying_type>(source));
18+
}
19+
20+
} // namespace bounded

0 commit comments

Comments
 (0)