Skip to content

Commit 90ad999

Browse files
committed
Add pointer_like<X> as X cast
1 parent 09e08ea commit 90ad999

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

include/cpp2util.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,18 @@ auto as( X* x ) -> C {
13521352
return Dynamic_cast<C>(x);
13531353
}
13541354

1355+
template <pointer_like T>
1356+
using pointee_t = CPP2_TYPEOF(*std::declval<T>());
1357+
1358+
template < typename C, pointer_like X>
1359+
requires std::same_as<pointee_t<X>, C>
1360+
auto as (X const & x) -> decltype(auto) {
1361+
if (x) {
1362+
return *x;
1363+
}
1364+
Throw( std::runtime_error("'as' cast failed for empty 'pointer_like'"), "'as' cast failed for empty 'pointer_like'");
1365+
}
1366+
13551367
//-------------------------------------------------------------------------------------------------------------
13561368
// std::variant is and as
13571369
//

0 commit comments

Comments
 (0)