Using SFINAE to check for global operator<<?

后端 未结 2 1193
你的背包
你的背包 2020-11-29 11:37

I want to have several overloaded, global to_string() functions that take some type T and convert it to its string representation. For the general

2条回答
  •  借酒劲吻你
    2020-11-29 12:11

    I should have simply been more faithful to this answer. A working implementation is:

    namespace has_insertion_operator_impl {
      typedef char no;
      typedef char yes[2];
    
      struct any_t {
        template any_t( T const& );
      };
    
      no operator<<( std::ostream const&, any_t const& );
    
      yes& test( std::ostream& );
      no test( no );
    
      template
      struct has_insertion_operator {
        static std::ostream &s;
        static T const &t;
        static bool const value = sizeof( test(s << t) ) == sizeof( yes );
      };
    }
    
    template
    struct has_insertion_operator :
      has_insertion_operator_impl::has_insertion_operator {
    };
    

    I believe that it does not actually rely on SFINAE.

提交回复
热议问题