I'm trying to make a function, which can 'print' content of array to any output object. It's looking a little bit like this:
template <class T, class Z>
void print(T* array, int& size, Z& obj)
{
for (int k = 0; k < size; k++)
Z<< array[k] << " ";
Z<< std::endl;
}
I want obj to be any output object like std::cout or any of fstream, so I could call it using:
print(arr_name, arr_size, std::cout)
or
std::ostream file;
print(arr_name, arr_size, file)
Unfortunately, in my current verion it doesn't work at all (errors involve '<<' operator). What's wrong? Is it even possible to create such function?
template <class Iter> void print(Iter begin, Iter end, std::ostream& os) { std::for_each(begin, end, [&](decltype(*begin) const& element) { os << element << "\n"; }); }. And use it with an array like this:int x[10]; print(begin(x), end(x), std::cout);int& sizeis nonsense (make itint size)