相关疑难解决方法(0)

Pretty-print C++ STL containers

Please take note of the updates at the end of this post.

Update: I have created a public project on GitHub for this library!


I would like to have a single template that once and for all takes care of pretty-printing all STL containers via operator<<. In pseudo code, I'm looking for something like this:

template<container C, class T, String delim = ", ", String open = "[", String close = "]">
std::ostream & operator<<(std::ostream & o, const C<T> …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading pretty-print c++11

379
推荐指数
5
解决办法
4万
查看次数

为什么C++找不到模板函数?

为什么我会收到编译错误no matching function for call to `f( __gnu_cxx::__normal_iterator > >)'

#include <vector>

template<typename T>
void f(const typename std::vector<T>::iterator &) {}

void g() {
  std::vector<int> v;
  f<int>(v.end());  // Compiles.
  f(v.end());  // Doesn't compile, gcc 4.3 can't find any match.
}
Run Code Online (Sandbox Code Playgroud)

最终,我想编写一个仅需要向量迭代器的函数,并且无法编译其他任何内容(带有有意义的错误)。所以这template<typename T>void f(const T&) {}不是一个好的解决方案,因为它也可以针对其他类型进行编译。

c++ templates function

2
推荐指数
1
解决办法
746
查看次数