Tech Off Post

Single Post Permalink

View Thread: Templated STL container pretty printer
  • User profile image
    KerrekSB

    That is genius, brilliant! If you post the solution on SO, I will accept it as the answer! I haven't read it closely yet, but I'm excited to find out how you did this.

    One thing, though: Can we add associative container support to print a map like "{1: hello, 2: world, 3: foobar}"? Oh, I see that it works for maps, but not for unordered_maps. Does the code require explicit predeclaration of all supported container types? Perhaps that could be wrapped into a macro for the client code...

    Maybe we can use SFINAE to determined automagically whether a class supports ::begin() and ::end()?!

    Edit: I think I have made a trait class to detect the presence of iterators!

     

    template<typename T>
    struct mycont_helper
    {
    private:
      template<typename C> static char test(typename C::iterator*);
      template<typename C> static int  test(...);
    public:
      static const bool __value = sizeof(test<T>(0)) == sizeof(char);
    };
    
    template<typename T>
    struct mycont : public std::integral_constant<bool, mycont_helper<T>::__value>
    { };

    Usage: mycont&lt;T&gt;::value will be true for containers and false otherwise.