All the standard implementations I have tried allow values to be assigned to the std::ostream_iterator‘s without having them dereferenced before the assignment. Although the standard algorithms dereference the iterators before the assignment, I would like to know why there are implementations that don’t just forbid the assignment statically (with the help of a proxy class) so that the compilation would just fail, so that the user knows something wrong might happen if such assignment is ported to another implementation not allowing the assignment for some reasons.
In general, when implementing a standard functionality, is it a good practice to limit the implementation to only allow what is explicitly mentioned by the standard?
#include <iterator>
#include <string>
#include <iostream>
using namespace std;
int main() {
ostream_iterator<string> o(cout);
o = "Hello World\n"; // o is not dereferenced! It compiles with my GCC environment
o++; // to make sure the implementation writes to cout
}
The operator overload that allows this assignment is specified by the C++ language standard. Thus, a C++ Standard Library implementation must provide it.
The overload is specified as follows (from C++11 §24.6.2.2/1):
(
Tis theTwith which theostream_iteratorwas instantiated. In your example, it isstring.)