Recursive AsString () for printing STL containers in C ++

I am trying to write an AsString () function that converts STL containers to string according to my taste. Here is the code that I have provided so far:

template<class T>
inline string AsString(const T& v);

template<class First, class Second>
inline string AsString(const pair<First, Second>& p);

template<class Iter>
inline string PrintSequence(const char* delimiters, Iter begin, Iter end) {
  string result;
  result += delimiters[0];
  int size = 0;
  for (size = 0; begin != end; ++size, ++begin) {
    if (size > 0) {
      result += ", ";
    }
    result += AsString(*begin);
  }
  result += delimiters[1];
  result += StringPrintf("<%d>", size);
  return result;
}

#define OUTPUT_TWO_ARG_CONTAINER(Sequence) \
template<class T1, class T2> \
inline string AsString(const Sequence<T1, T2>& seq) { \
  return PrintSequence("[]", seq.begin(), seq.end()); \
}

OUTPUT_TWO_ARG_CONTAINER(vector)
OUTPUT_TWO_ARG_CONTAINER(deque)
OUTPUT_TWO_ARG_CONTAINER(list)

template<class First, class Second>
inline string AsString(const pair<First, Second>& p) {
  return "(" + AsString(p.first) + ", " + AsString(p.second) + ")";
}

template<class T>
inline string AsString(const T& v) {
  ostringstream s;
  s << v;
  return s.str();
}

As you can see, the main idea is that it AsString()calls itself recursively in STL containers, and then it comes to the usual one operator<<()(the reason I don’t want to override operator<<()is because I don’t want to interfere with other libraries that do exactly this.

Now it AsString()compiles and works on small containers, but not on nested ones:

vector<int> v;
v.push_back(1);
v.push_back(2);
AsString(v) == "[1, 2]<2>";  // true

vector<vector<int> > m;
m.push_back(v);
m.push_back(v);
AsString(m) == "[[1, 2]<2>, [1, 2]<2>]<2>";  // Compilation Error!!!

For some reason, the compiler wants to use the operator<<()`m 'elements when trying to print, despite the fact that I have provided a specialized specialization for vectors.

AsString() ?

UPDATE: , , ( - gcc 4.4.3). , . .

+3
2

... ...

, .

( , ) .

template <typename T>
void foo(T const& t);

template <>
void foo<int>(int i); // this is a "complete" specialization

template <typename T, typename U>
void foo<std::pair<T,U>>(std::pair<T,U> const& pair);
  // this is a "partial" specialization
  // and by the way... it does NOT COMPILE

template <typename T, typename U>
void foo(std::pair<T,U> const& pair); // this is an overload

, <xxxx> (foo ).

++ ; . , : GotW # 49:

, :

template <typename T>
std::string AsString(const T& v); // (1)

template <typename T, typename Allocator>
std::string AsString(std::vector<T, Allocator> const& v); // (2)

: *begin?

, m :

  • Iter std::vector< std::vector<int> >::iterator.
  • *begin, , std::vector<int>&

, :

  • (1): T = std::vector<int>, const-ref
  • (2): T = int, U = std::allocator<int>, const-ref

, , . V++ 2010, .

, ? ( , ;)).

+3

, AsString. , T const & .

op < < stdlib. , , AsString:

namespace make_sure_to_put_these_overloads_in_a_namespace {

// Your PrintSequence adapted to a stream instead of a string:
template<class Iter>
void PrintSequence(std::ostream &s, const char* delim,
                   Iter begin, Iter end)
{
  s << delim[0];
  int size = 0;
  if (begin != end) {
    s << *begin;
    ++size;
    while (++begin != end) {
      s << ", " << *begin;
      ++size;
    }
  }
  s << delim[1] << '<' << size << '>';
}

#define OUTPUT_TWO_ARG_CONTAINER(Sequence) \
template<class T1, class T2> \
std::ostream& operator<<(std::ostream &s, Sequence<T1, T2> const &seq) { \
  PrintSequence(s, "[]", seq.begin(), seq.end()); \
  return s; \
}

OUTPUT_TWO_ARG_CONTAINER(std::vector)
OUTPUT_TWO_ARG_CONTAINER(std::deque)
OUTPUT_TWO_ARG_CONTAINER(std::list)
// other types
#undef OUTPUT_TWO_ARG_CONTAINER

template<class First, class Second>
std::ostream& operator<<(std::ostream &s, std::pair<First, Second> const &p) { \
  s << "(" << p.first << ", " << p.second << ")";
  return s;
}

}

template<class T>
std::string AsString(T const &v) {
  using namespace make_sure_to_put_these_overloads_in_a_namespace;
  std::ostringstream ss;
  ss << v;
  return ss.str();
}
+1

Source: https://habr.com/ru/post/1788275/


All Articles