What is a recommended way to overload the output stream operator? The following can not be done. It is expected that compilation will fail if the operator << is not defined for a type T.
template < class T >
inline std::ostream& operator << (std::ostream& os, const std::vector<T>& v)
{
os << "[";
for (std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
{
os << " " << *ii;
}
os << " ]";
return os;
}
EDIT: It does compile, the problem was unrelated and was in the namespace. Thanks for assistance.
Did you actually try this code? It works fine on gcc with a small tweak
std::vector<T>::const_iterator, needs to be declared astypename std::vector<T>::const_iteratorYou may be better off with using std::copy and std::ostream_iterator.
EDIT: types, dependent types and typename
Can’t fit it all in the comments, so here goes (btw. this is my understanding and I could be off by a country mile – if so please correct me!)…
I think this is best explained with a simple example..
Let’s assume you have a function foo
Looks okay, and typically you may do this
And call
Again, seems self explanatory, however some nuser comes along and does this
Now
What you have now is an expression (multiplication) as IdiotClass::bob resolves to a non-type!
To the human, it’s obvious that this is stupid, but the compiler has no way of differentiating between types vs. non-types, and by default in C++ (and I think this is where compilers differ), all qualified dependent names (i.e. T::bob) will be treated as non-type. To explicitly tell the compiler that the dependent name is a real type, you must specify the
typenamekeyword –This applies even if it is a
typedef. i.e.Is that any clearer?