Skip to content

Commit f964b10

Browse files
committed
[cpp2util] as: add constexpr branch for optionals
1 parent 00d5047 commit f964b10

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

include/cpp2util.h

+12
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,15 @@ auto as(X const& x CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> decltype(auto) {
13531353
}
13541354
}
13551355

1356+
template<typename T>
1357+
struct is_optional : std::false_type {};
1358+
1359+
template<typename T>
1360+
struct is_optional<std::optional<T>> : std::true_type {};
1361+
1362+
template<typename T>
1363+
constexpr auto is_optional_v{is_optional<T>::value};
1364+
13561365
template< typename C, typename X >
13571366
auto as( X& x ) -> decltype(auto) {
13581367
if constexpr (std::is_same_v<C, X>) {
@@ -1364,6 +1373,9 @@ auto as( X& x ) -> decltype(auto) {
13641373
else if constexpr (std::is_base_of_v<X, C>) {
13651374
return Dynamic_cast<C&>(x);
13661375
}
1376+
else if constexpr (is_optional_v<X>) {
1377+
return x.value();
1378+
}
13671379
else {
13681380
return as<C>(std::as_const(x));
13691381
}

0 commit comments

Comments
 (0)