Skip to content

Commit f15e640

Browse files
Merge pull request #5 from contour-terminal/improvement/disambiguous_unbox
Add SFINAE for plain unbox instead of template
2 parents daa702e + 6e46e1f commit f15e640

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ set(boxed_cpp_HEADERS
1313
)
1414
add_library(boxed-cpp INTERFACE)
1515

16-
target_compile_features(boxed-cpp INTERFACE cxx_std_17)
16+
target_compile_features(boxed-cpp INTERFACE cxx_std_20)
1717
target_include_directories(boxed-cpp INTERFACE
1818
$<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include>
1919
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/include>

include/boxed-cpp/boxed.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,17 @@ constexpr auto unbox(boxed::boxed<From, FromTag> const& from) noexcept
164164
return static_cast<To>(from.value);
165165
}
166166

167+
template <typename T>
168+
concept con_boxed = requires(T t)
169+
{
170+
typename T::inner_type;
171+
};
172+
167173
// Casting a boxed type out of the box.
168-
template <typename From, typename FromTag>
169-
constexpr auto unbox(boxed::boxed<From, FromTag> const& from) noexcept
174+
template <con_boxed T>
175+
constexpr auto unbox(T from) noexcept
170176
{
171-
return unbox<From,From,FromTag>(from);
177+
return unbox<typename T::inner_type>(from);
172178
}
173179

174180
namespace std

test-boxed-cpp.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,11 @@ TEST_CASE("cast inside rvalue")
110110
double distance_d = speed_of_light * 2.0;
111111
REQUIRE(distance_d - 2.0 * 299792458.0 < std::numeric_limits<double>::epsilon());
112112
}
113+
114+
TEST_CASE("all options for unbox")
115+
{
116+
auto speed_of_light = Speed(299792458.0);
117+
REQUIRE(unbox<double>(speed_of_light));
118+
REQUIRE(unbox<float>(speed_of_light));
119+
REQUIRE(unbox(speed_of_light));
120+
}

0 commit comments

Comments
 (0)