Modified November 4, 2003
There are examples in my book that are in error according to the standard but that I haven't changed because there is a defect report on the issue.
Issues marked by * have been resolved so that the standard agrees with my book. The issue marked ** was resolved in favor of the draft standard.
ostream_iterator< string> oo(os);I got caught by using an old version of STL where ostream_iterator was defined like this:
template < class T>
class ostream_iterator : public output_iterator {
protected:
ostream* stream;
char* string;
// ...
};
The standards conforming version (pg 560) looks like this:
template < class T, class Ch, class Tr = char_traits< Ch> >
class ostream_iterator : public iterator< output_iterator_tag,void,void,void,void> {
public:
typedef Ch char_type;
typedef Tr traits_type;
typedef basic_ostream< Ch,Tr> ostream_type;
// ...
};
Consequently, I find that my code using istream_iterators and ostream_iterators
is broken according to the standard if not for most current implementations.
For example:
ostream_iterator< string> oo(os); // error: too few template argumentsIt should be:
ostream_iterator< string,char> oo(os);The problem appears to be caused by an accidentally incomplete conversion of ostream_iterator to use templatized streams. I think the proper definition ought to be:
template < class T, class Ch = char, class Tr = char_traits< Ch> >
class ostream_iterator : public iterator< output_iterator_tag,void,void,void,void> {
public:
typedef Ch char_type;
typedef Tr traits_type;
typedef basic_ostream< Ch,Tr> ostream_type;
// ...
};
I have raised the issue in the standards committee and hope to see it resolved soon.
char vc2[200]; copy(&vc1[0],&vc1[200],&vc2[200]);The issue is whether taking the address of one-past-the-last element of an array is conforming C and C++. I could make the example clearly conforming by a simple rewrite:
copy(vc1,vc1+200,vc2+200);However, I don't want to introduce addition to pointers at this point of the book. It is a surprise to most experienced C and C++ programmers that &vc2[200] isn't completely equivalent to vc2+200. In fact, it was a surprise to the C committee also and I expect it to be fixed in the upcoming revision of the standard. (resolved for C9x - bs 10/13/98). (this means that the example was ok in K&R C, in ARM C++, an error in C89 and ISO C++, and ok in C9x - a thorny issue).
templateI consider for_each() overconstrained and use it in ways that goes beyond what is guaranteed by the draft standard. I hope/expect that the requirement will be relaxed.Op for_each(In first, In last, Op f) { while (first != last) f(*first++); return f; }
#include < iostream>
int main()
{
std::cout << "Hello world" << std::endl;
return 0;
}
Personally, I prefer the simpler (and valid)
#include < iostream>
int main()
{
std::cout << "Hello world\n";
}
but I expect that there are enough programs depending on endl for the
committee to seriously consider changing the standard to match the
implementations.
ostringstream ost("Hello, ");
ost << "world!";
According to my reading of the standard, this should output "Hello, world!".
The claim is that ignoring the initializer and producing the output "world "
is what was intended (eventhough the standards text doesn't explicitly say so).
Resolution: That example prints "world! " If you want, "Hello, world!" say
ostringstream ost("Hello, ",ios_base::ate); // open to write "at end"
ost << "world!";
Note that this makes a string stream behave for a string analogously to the
way a file stream behaves for a file.