Is it possible to create a trait to answer if a type comes from std?

后端 未结 1 599
名媛妹妹
名媛妹妹 2021-01-12 15:48

After this question by utilizing ADL one can create a trait to answer if the passed type comes from our namespace:

#include 

namespace helper         


        
1条回答
  •  时光说笑
    2021-01-12 16:34

    This seems to work:

    #include 
    #include 
    #include 
    #include 
    
    namespace other { struct S{}; }
    
    namespace my {
        template< class Type >
        void ref( Type&& ) {}
    
        template< class Type >
        auto ref_to( Type&& o )
            -> Type&
        { return o; }
    
        template< class Type >
        constexpr auto is_std_type()
            -> bool
        {
            using std::is_same;
            using std::declval;
            return not is_same< void, decltype( ref( ref_to( declval() ) ) )>::value;
        }
    
        struct Blah {};
    
        constexpr bool int_is_std       = is_std_type();
        constexpr bool blah_is_std      = is_std_type();
        constexpr bool other_is_std     = is_std_type();
        constexpr bool string_is_std    = is_std_type();
    };
    
    #include 
    using namespace std;
    auto main()
        -> int
    {
        cout << boolalpha;
        cout << "int is std = " << my::int_is_std << "\n";
        cout << "blah is std = " << my::blah_is_std << "\n";
        cout << "other is std = " << my::other_is_std << "\n";
        cout << "string is std = " << my::string_is_std << "\n";
    }
    

    0 讨论(0)
提交回复
热议问题