Topic: Stroustrup's code
Author: "Richard Muller" <rlmuller@msn.com>
Date: 1997/10/12 Raw View
Bjarne,
I just experienced Freudenshade (which I is German for experiencing joy at
someone else's misfortune) when I saw that your comments about a mistake in
your 3rd Edition. As you surely know, we lesser lights in the programming
community make tons of mistakes.
I just got your new edition and have thoroughly enjoyed your "tour" of the
langage. I have wanted to improve my use of exceptions but had been
discourage by Microsoft's incomplete (it seems to me) implementation of
user defined exceptions. I've been restricting my coding to char *
exception types, which did the job to some extert. Your treatment of the
topic has motivated to revist this issue.
Thanks for the toil you and your friends have put in over the past two
decades in bringing C++ to life. To repeat what I just emailed to Andrew
Koenig, the fruits of your efforts have given me a lot of intellectual,
professional and recreational rewards over the years.
Best wishes,
Richard Muller
Bjarne Stroustrup <bs@research.att.com> wrote in article
<EGrMHD.HGL@research.att.com>...
>
> and I - belatedly and embarrasingly - find that all of my code is broken
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1997/09/18 Raw View
Hi,
Reed Mangino (mangino@saturn.planet.net) wrote:
: On page 61 he demonstrates input_iterators/output_iterators as follows:
: int main()
: {
: string from, to; // two filenames
: cin >> from >> to;
: ifstream is(from.c_str());
: istream_iterator<string> ii(is);
: istream_iterator<string> eos; // empty string (used like EOF)
: vector<string> b(ii, eos); // (*) read all strings from the file
: // into the vector b
: }
: I have not been able to get the above code to compile using MSVC++ 5.0 nor
: G++ 2.7.2.1 (under Linux). I have also tried Objectspace's standard c++
: library...
Well, if you use 'copy()' instead of relying on the constructor it would
work. Just replace the the last line with these two:
vector<string> b;
copy(ii, eos, back_inserter(b);
However, the problem with the code is actually worse than that: Unless
Mr.Stroustrup knows more about the forthcoming standard, the
'istream_iterator's are declared wrong: 'istream_iterator' has TWO
required tempate arguments (see 24.5.1, lib.istream.iterator, paragraph
2 of the Nov '96 DWP)! ... and I doubt that this changed recently.
: Stroustrup states that he compiled the code on various implementations.
: Anyone know which ones support this type of definitions??
But then, I guess that most implementation supporting member templates
will currently compile Stroustrup's code and I also think that the
Standard should be changed to use 'char' as default argument for the
second template argument of 'istream_iterator' (and
'ostream_iterator)...
--
<mailto:dietmar.kuehl@uni-konstanz.de>
<http://www.informatik.uni-konstanz.de/~kuehl/>
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: bs@research.att.com (Bjarne Stroustrup)
Date: 1997/09/20 Raw View
kuehl@horn.informatik.uni-konstanz.de (Dietmar Kuehl) writes:
>
> Hi,
> Reed Mangino (mangino@saturn.planet.net) wrote:
> : On page 61 he demonstrates input_iterators/output_iterators as follows:
>
> : int main()
> : {
> : string from, to; // two filenames
> : cin >> from >> to;
>
> : ifstream is(from.c_str());
> : istream_iterator<string> ii(is);
> : istream_iterator<string> eos; // empty string (used like EOF)
>
> : vector<string> b(ii, eos); // (*) read all strings from the file
> : // into the vector b
> : }
>
> : I have not been able to get the above code to compile using MSVC++ 5.0 nor
> : G++ 2.7.2.1 (under Linux). I have also tried Objectspace's standard c++
> : library...
>
> Well, if you use 'copy()' instead of relying on the constructor it would
> work. Just replace the the last line with these two:
>
> vector<string> b;
> copy(ii, eos, back_inserter(b);
>
> However, the problem with the code is actually worse than that: Unless
> Mr.Stroustrup knows more about the forthcoming standard, the
> 'istream_iterator's are declared wrong: 'istream_iterator' has TWO
> required tempate arguments (see 24.5.1, lib.istream.iterator, paragraph
> 2 of the Nov '96 DWP)! ... and I doubt that this changed recently.
Ouch! I got caught by using an old version of STL. Consider ostream_iterator;
it was defined like this:
template <class T>
class ostream_iterator : public output_iterator {
protected:
ostream* stream;
char* string;
// ...
};
The standards conforming version 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;
// ...
};
and I - belatedly and embarrasingly - find that all of my code is broken
(according to the standard if not for most current implementations).
For example:
ostream_iterator<string> os(cout); // error: too few template arguments
It should be:
ostream_iterator<string,char> os(cout);
Tough, you might say, but it appears to be needlessly broken by an incomplete
conversion to 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;
// ...
};
and I have raised the issue in the standards committee.
> : Stroustrup states that he compiled the code on various implementations.
> : Anyone know which ones support this type of definitions??
I said (pg 6 of "The C++ Programming Language (3rd Edition)"):
"The language used in this book is ``pure C++'' as defined in the C++ standard.
Therefore, the examples ought to run on every \*C implementation. The major
program fragments in this book were tried using several C++ implementations.
Examples using features only recently adopted into C++ didn't compile on every
implementation. However, I see no point in mentioning which implementations
failed to compile which examples. Such information would soon be out of date
because implementers are working hard to ensure that their implementations
correctly accept every C++ feature. See Appendix B for suggestions on how to
cope with older C++ compilers and with code written for C compilers."
> But then, I guess that most implementation supporting member templates
> will currently compile Stroustrup's code and I also think that the
> Standard should be changed to use 'char' as default argument for the
> second template argument of 'istream_iterator' (and
> 'ostream_iterator)...
I agree. The issue is whether it is possible to do so at this late stage
(until I hear otherwise, I will assume that a possibility exists).
- Bjarne
Bjarne Stroustrup, AT&T Labs, http://www.research.att.com/~bs
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Brock Peabody <npcis@pitton.com>
Date: 1997/09/17 Raw View
I am using MSVC 5.0, and I think that the problem is that Stroustrup's
code uses a vector constructor that is a template function, but MSVC
5.0's STL implementation does not use them. I do not know why, as you
can define your own classes with template members.
Reed Mangino wrote:
>
> I have been reading through Stroustrup's _The C++ Programming Language
> 3rd Edition_.
>
> On page 61 he demonstrates input_iterators/output_iterators as follows:
>
> int main()
> {
> string from, to; // two filenames
> cin >> from >> to;
>
> ifstream is(from.c_str());
> istream_iterator<string> ii(is);
> istream_iterator<string> eos; // empty string (used like EOF)
>
> vector<string> b(ii, eos); // (*) read all strings from the file
> // into the vector b
> }
>
> I have not been able to get the above code to compile using MSVC++ 5.0 nor
> G++ 2.7.2.1 (under Linux). I have also tried Objectspace's standard c++
> library...
>
> The compiler dies at the vector definition. For some reason it keeps
> trying to use 'vector ( size_type n, const T& value )' instead of
> 'vector(const_iterator first, const_iterator last,
> const OS_ALLOCATOR( T ) & alloc = OS_ALLOCATOR( T )()
> )
>
> Stroustrup states that he compiled the code on various implementations.
> Anyone know which ones support this type of definitions??
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: bkline@nlm.nih.gov (Bob Kline)
Date: 1997/09/17 Raw View
Reed:
I believe the problem is caused by the fact that the compilers which
fail to compile the code as written have not implemented the correct
constructor in their version of the standard library. The constructor
should be:
template <class InputIterator>
vector(InputIterator first, InputIterator last, const A& al = A());
Instead they're implementing it as
vector(const_iterator first, const_iterator last, const A& al = A());
Usually (always?) this is because the compiler in question cannot
handle member template functions.
FWIW (not much, I realize) I haven't found a compiler which handles
the code as written, either. I'm afraid we're in for a painful
stretch while we wait for the compilers to catch up.
Bob
Reed Mangino (mangino@saturn.planet.net) wrote:
: I have been reading through Stroustrup's _The C++ Programming Language
: 3rd Edition_.
: On page 61 he demonstrates input_iterators/output_iterators as follows:
: int main()
: {
: string from, to; // two filenames
: cin >> from >> to;
: ifstream is(from.c_str());
: istream_iterator<string> ii(is);
: istream_iterator<string> eos; // empty string (used like EOF)
: vector<string> b(ii, eos); // (*) read all strings from the file
: // into the vector b
: }
: I have not been able to get the above code to compile using MSVC++ 5.0 nor
: G++ 2.7.2.1 (under Linux). I have also tried Objectspace's standard c++
: library...
: The compiler dies at the vector definition. For some reason it keeps
: trying to use 'vector ( size_type n, const T& value )' instead of
: 'vector(const_iterator first, const_iterator last,
: const OS_ALLOCATOR( T ) & alloc = OS_ALLOCATOR( T )()
: )
: Stroustrup states that he compiled the code on various implementations.
: Anyone know which ones support this type of definitions??
: Thank you for your time,
: Reed
: --
: ^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^^-__-^^
: Reed R. Mangino | ** Dialogic Corporation **
: manginor@dialogic.com | World leader in the design of
: mangino@planet.net | computer telephony systems
: -----------------------------------------------------------
: ---
: [ comp.std.c++ is moderated. To submit articles: Try just posting with your
: newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
: comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
: Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
: Comments? mailto:std-c++-request@ncar.ucar.edu
: ]
--
Bob Kline
E:bob_kline@corpsoft.com
V:703.522.0820 x-311
F:703.522.5407
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: mangino@saturn.planet.net (Reed Mangino)
Date: 1997/09/10 Raw View
I have been reading through Stroustrup's _The C++ Programming Language
3rd Edition_.
On page 61 he demonstrates input_iterators/output_iterators as follows:
int main()
{
string from, to; // two filenames
cin >> from >> to;
ifstream is(from.c_str());
istream_iterator<string> ii(is);
istream_iterator<string> eos; // empty string (used like EOF)
vector<string> b(ii, eos); // (*) read all strings from the file
// into the vector b
}
I have not been able to get the above code to compile using MSVC++ 5.0 nor
G++ 2.7.2.1 (under Linux). I have also tried Objectspace's standard c++
library...
The compiler dies at the vector definition. For some reason it keeps
trying to use 'vector ( size_type n, const T& value )' instead of
'vector(const_iterator first, const_iterator last,
const OS_ALLOCATOR( T ) & alloc = OS_ALLOCATOR( T )()
)
Stroustrup states that he compiled the code on various implementations.
Anyone know which ones support this type of definitions??
Thank you for your time,
Reed
--
^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^^-__-^^
Reed R. Mangino | ** Dialogic Corporation **
manginor@dialogic.com | World leader in the design of
mangino@planet.net | computer telephony systems
-----------------------------------------------------------
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]