Why there are no std::wostream_iterator in C++?
Is there any good reason for this?
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
int main()
{
std::vector<std::wstring> myvec = { L"first", L"second" };
std::wofstream f("New.txt");
// std::copy(myvec.begin(), myvec.end(), std::wostream_iterator<std::wstring>(f)); // Error
// std::copy(myvec.begin(), myvec.end(), std::ostream_iterator<std::wstring>(f)); // Error
std::copy(myvec.begin(), myvec.end(), std::ostream_iterator<std::wstring, wchar_t>(f)); // Ok
std::copy(myvec.begin(), myvec.end(), std::ostream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t>>(f)); // Ok
}
Because
std::ostream_iteratoris nottypedef(andstd::wstringis typedef onstd::basic_stringwithcharT=wchar_t).Second copy operation
is incorrect, since
so, in this case
ostream_typeisbasic_ostream<char, std::char_traits<char> >Constructor can receive only this type, but we pass
basic_ofstream<wchar_t, std::char_traits<wchar_t> >, it’s incorrect.