FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improved `as<T>` to remove ambiguities and add the missing base/catch… · feiyunwill/cppfront@d7a6c99 · GitHub

Commit d7a6c99

Browse files
committed
Improved as<T> to remove ambiguities and add the missing base/catchall case
1 parent d424545 commit d7a6c99

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

‎include/cpp2util.h‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,17 @@ auto is( X const* x ) -> bool {
366366
//-------------------------------------------------------------------------------------------------------------
367367
// Built-in as (partial)
368368
//
369+
template< typename C >
370+
auto as(...) -> auto {
371+
return nonesuch;
372+
}
373+
374+
template< typename C, typename X >
375+
requires std::is_same_v<C, X>
376+
auto as( X const& x ) -> auto&& {
377+
return x;
378+
}
379+
369380
template< typename C, typename X >
370381
requires std::is_base_of_v<C, X>
371382
auto as( X&& x ) -> C&& {
@@ -448,29 +459,28 @@ auto as( std::variant<Ts...> const& x ) {
448459
//-------------------------------------------------------------------------------------------------------------
449460
// std::any is and as
450461
//
451-
template<typename T>
452-
constexpr auto is( std::any const& x ) -> bool
462+
template<typename T, typename X>
463+
requires std::is_same_v<X,std::any>
464+
constexpr auto is( X const& x ) -> bool
453465
{ return x.type() == typeid(T); }
454466

455-
template<typename T> requires (!std::is_reference_v<T>)
456-
constexpr auto as( std::any const& x ) -> T
467+
template<typename T, typename X>
468+
requires (!std::is_reference_v<T> && std::is_same_v<X,std::any>)
469+
constexpr auto as( X const& x ) -> T
457470
{ return std::any_cast<T>( x ); }
458471

459472

460473
//-------------------------------------------------------------------------------------------------------------
461474
// std::optional is and as
462475
//
463-
template<typename T>
464-
constexpr auto is( std::optional<T> const& x ) -> bool
476+
template<typename T, typename X>
477+
requires std::is_same_v<X,std::optional<T>>
478+
constexpr auto is( X const& x ) -> bool
465479
{ return x.has_value(); }
466480

467-
template<typename T, typename U>
468-
requires (!std::is_same_v<T,U>)
469-
constexpr auto is( std::optional<U> const& x ) -> bool
470-
{ return false; }
471-
472-
template<typename T>
473-
constexpr auto as( std::optional<T> const& x ) -> auto&&
481+
template<typename T, typename X>
482+
requires std::is_same_v<X,std::optional<T>>
483+
constexpr auto as( X const& x ) -> auto&&
474484
{ return x.value(); }
475485

476486

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL