Error in Sutter book?

L

loufoque

Noah said:
(argc > 2
? ofstream(argv[2], ios::eek:ut | ios::binary)
: cout)

This is not equivalent to

ofstream out(argv[2], ios::eek:ut | ios::binary);

(argc > 2 ? out : cout)
 
N

Noah Roberts

Herb said:
I'm not sure what it says in your printing, but IIRC in should have type
ifstream and out should have type ofstream. Then the code compiles fine
under most modern compilers (incl. EDG and gcc 3.x and 4.x).

I have the 8th printing, Aug. 2006, and it says "fstream in, out;" for
the declaration in the main function that uses Process from examples
1-2 a and b.

However, even if I use those types I still get the same result
(altering the first example a bit):


#include <iostream>
#include <fstream>

int main(int argc, char* argv[])
{
using namespace std;

ifstream f(argv[1], ios::in | ios::binary);

(argc > 2
? cout
: cout)
<<
(argc > 1
? f
: cin)
.rdbuf();
}

I'm aware of all the potential errors here, I'm just looking for a
compile.

Of course, that isn't really any different from the original...

It seems the ternary operator has decided to do some sort of conversion
that isn't working even for the explicit ixxx/oxxx types. It just
doesn't want to handle that type of use...that's the point of failure
for any type of fstream and cin/cout.
It also worked fine under VC++ 6 (current when the book was published),
but oddly it apparently doesn't under VC++ 7.1 (2003) and 8.0 (2005). Hmm.
I'll look into it. In the meantime, a workaround would be to not use the
cuteness of the ternary operator, and work with a pointer instead:

ifstream in;
basic_istream<char>* i = &cin;

ofstream out;
basic_ostream<char>* o = &cout;

if( argc > 1 ) {
in.open ( argv[1], ios::in | ios::binary );
i = &in;
}

if( argc > 2 ) {
out.open( argv[2], ios::eek:ut | ios::binary );
o = &out;
}

Process( *i, *o );


Yeah, that will work. I was just curious which is right, you or the
compiler. When you figure it out I would very much like to know and
you can post to my personal email, (e-mail address removed), if you wish.

Here's the compiler error for completeness. It happens any time
cin/cout are on one side and any type of fstream is on the other. It
handles pointers just fine though.


1>c:\program files\microsoft visual studio 8\vc\include\istream(842) :
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access
private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files\microsoft visual studio
8\vc\include\ios(151) : see declaration of
'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function
'std::basic_istream<_Elem,_Traits>::basic_istream(const
std::basic_istream<_Elem,_Traits> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
 
N

Noah Roberts

Alf said:
* Noah Roberts:

No, I don't have that book.

I don't think I will ever understand why some people choose to play
this kind of game. You can claim you don't know but we both know you
do, and did all along, but for some reason want to pretend you don't
and so we have to take 50 steps back into pedant mode and pretend to
bring you up to speed. There's no reason for it really; it serves no
other purpose than to slow down discussion and I honestly don't get it.
My guess is that it is some sort of power play mechanism but I don't
really understand why you would feel the need for that with me. I find
people that choose this method of communication very frustrating to
work with.
Next, the roles of E1 and E2 need to be switched and this new situation
analysed, but there's no way to convert std::eek:stream down to std::eek:fstream.

CC: Herb Sutter.

Thank you for your eventual answer.
 
A

Alf P. Steinbach

* Noah Roberts:
I don't think I will ever understand why some people choose to play
this kind of game. You can claim you don't know but we both know you
do, and did all along, but for some reason want to pretend you don't
and so we have to take 50 steps back into pedant mode and pretend to
bring you up to speed. There's no reason for it really; it serves no
other purpose than to slow down discussion and I honestly don't get it.
My guess is that it is some sort of power play mechanism but I don't
really understand why you would feel the need for that with me. I find
people that choose this method of communication very frustrating to
work with.

It's necessary to be precise, exact, when discussing a possible error
(or for that matter, any technical issue).

I'm not a telepath able to see at a distance what you've read in the
book, and when something isn't defined, it can be anything.

Hence, the request for clarification, and for a complete example.

Thank you for your eventual answer.

You're welcome.
 
B

BobR

Noah Roberts wrote in message ...
When you figure it out I would very much like to know and
**you can post to my personal email**, if you wish.

Please don't, there are others who may want to know!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top