473,657 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error in Sutter book?

Item #1 in the More Exceptional C++ book uses the following construct:

fstream in;

....

process( in.is_open() ? in : cin,...);
Where process has been shown as having various multiple different
possible definitions. Among them is:

void process(basic_i stream<char>& in,..);

I get errors when I try to do this in visual studio. It looked like an
erroneous construct to me to begin with (using ternary with two
different types being passed back) so I wanted to verify that it
actually worked and it doesn't. I also tried something like so:

istream & s = in.is_open() ? in : cin;

no go.

Working with pointers does work:

istream * s = in.is_open() ? &in : &cin;

This does not:

istream * s = &(in.is_open () ? in : cin);

Is the compiler breaking the standard here or is the use of the ternary
operator flawed?

Another definition he has is:

template < typename In, typename Out>
void process(In & in, Out & out)
{
....
}

But again, how can the ternary operator be used as such:

process(in.is_o pen() ? in : cin, ...)

I suppose the compiler could be really smart and derive a template
based on istream instead of either fstream or whatever cin is but I
don't think there is such a thing, is there? Or it could create two
instances of process and create a totally different branching
construct...aga in, I don't know of any that would and that seems on the
outside of qualifying for "as if".

But if I am right, how did this make it into such a high profile book
by such a high profile author...so I question my understanding.

Nov 15 '06 #1
24 2133

Noah Roberts wrote:
istream & s = in.is_open() ? in : cin;
I suppose the error recieved might be of benefit. It is the same for
all uses (except the one pointer use that works) of the ternary
operator in my op.
1>c:\program files\microsoft visual studio 8\vc\include\is tream(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::ch ar_traits<char>
1 ]
1 c:\program files\microsoft visual studio
8\vc\include\io s(151) : see declaration of
'std::basic_ios <_Elem,_Traits> ::basic_ios'
1 with
1 [
1 _Elem=char,
1 _Traits=std::ch ar_traits<char>
1 ]
1 This diagnostic occurred in the compiler generated function
'std::basic_ist ream<_Elem,_Tra its>::basic_ist ream(const
std::basic_istr eam<_Elem,_Trai ts&)'
1 with
1 [
1 _Elem=char,
1 _Traits=std::ch ar_traits<char>
1 ]

Nov 15 '06 #2
Noah Roberts wrote:
Item #1 in the More Exceptional C++ book uses the following construct:

fstream in;

...

process( in.is_open() ? in : cin,...);
Where process has been shown as having various multiple different
possible definitions. Among them is:

void process(basic_i stream<char>& in,..);

I get errors when I try to do this in visual studio.
Works fine for me with both GCC and Comeau.
Nov 15 '06 #3
* Noah Roberts:
Item #1 in the More Exceptional C++ book uses the following construct:

fstream in;

...

process( in.is_open() ? in : cin,...);
Where process has been shown as having various multiple different
possible definitions. Among them is:

void process(basic_i stream<char>& in,..);

I get errors when I try to do this in visual studio. It looked like an
erroneous construct to me to begin with (using ternary with two
different types being passed back) so I wanted to verify that it
actually worked and it doesn't. I also tried something like so:

istream & s = in.is_open() ? in : cin;

no go.

Working with pointers does work:

istream * s = in.is_open() ? &in : &cin;

This does not:

istream * s = &(in.is_open () ? in : cin);

Is the compiler breaking the standard here or is the use of the ternary
operator flawed?
Depends whether 'in' and 'cin' are of identical type. If they are, and
both are lvalues, the result is an lvalue. Otherwise the result is an
rvalue, which implies copying, which is meaningless for streams.
[snip]
But if I am right, how did this make it into such a high profile book
by such a high profile author...so I question my understanding.
Check the errate pages. Perhaps there's something.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 15 '06 #4
* Noah Roberts:
>
But if I am right, how did this make it into such a high profile book
by such a high profile author...so I question my understanding.
I was just skimming the 2nd edition of "The C++ Programming Language" by
Bjarne Stroustrup. Here 'main' is variously declared as

int main() {}
main() {}
void main() {}

Nobody's prefect.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 15 '06 #5

Alf P. Steinbach wrote:
* Noah Roberts:
Item #1 in the More Exceptional C++ book uses the following construct:

fstream in;

...

process( in.is_open() ? in : cin,...);
Is the compiler breaking the standard here or is the use of the ternary
operator flawed?

Depends whether 'in' and 'cin' are of identical type. If they are, and
both are lvalues, the result is an lvalue. Otherwise the result is an
rvalue, which implies copying, which is meaningless for streams.
Since cin is set as an istream by the standard and in is an fstream
they are obviously not the same type even though they are related
types.
>

[snip]
But if I am right, how did this make it into such a high profile book
by such a high profile author...so I question my understanding.

Check the errate pages. Perhaps there's something.
It isn't there.

Nov 15 '06 #6

Alf P. Steinbach wrote:
* Noah Roberts:

But if I am right, how did this make it into such a high profile book
by such a high profile author...so I question my understanding.

I was just skimming the 2nd edition of "The C++ Programming Language" by
Bjarne Stroustrup. Here 'main' is variously declared as

int main() {}
main() {}
void main() {}

Nobody's prefect.
That's a rather minor error though. This one, if it is an error,
tosses out the entire advice given in that section. The first 5 pages
or so of the book become completely erroneous. This is why I'm trying
to figure out why I'm wrong and the book is right even though it
obviously doesn't compile on at least one pretty common compiler.

Nov 15 '06 #7
Item #1 in the More Exceptional C++ book uses the following construct:
>
fstream in;

...

process( in.is_open() ? in : cin,...);

Regrettably, I don't have a copy of this book, but could it be that it
declares the variable 'in' as an ifstream instead of an fstream? That
would seem to make sense in light of the downstream code
void process(basic_i stream<char>& in,..);
Michael

Nov 15 '06 #8
Alf P. Steinbach wrote:
* Noah Roberts:

But if I am right, how did this make it into such a high profile
book by such a high profile author...so I question my understanding.

I was just skimming the 2nd edition of "The C++ Programming Language"
by Bjarne Stroustrup. Here 'main' is variously declared as

int main() {}
main() {}
void main() {}

Nobody's prefect.
The second edition was pre-standard, I believe. The first two forms
were probably correct at the time, but I don't know about the third.
Consistency would probably have been better.


Brian
Nov 15 '06 #9
* Noah Roberts:
Alf P. Steinbach wrote:
>* Noah Roberts:
>>Item #1 in the More Exceptional C++ book uses the following construct:

fstream in;

...

process( in.is_open() ? in : cin,...);
>>Is the compiler breaking the standard here or is the use of the ternary
operator flawed?
Depends whether 'in' and 'cin' are of identical type. If they are, and
both are lvalues, the result is an lvalue. Otherwise the result is an
rvalue, which implies copying, which is meaningless for streams.

Since cin is set as an istream by the standard and in is an fstream
they are obviously not the same type even though they are related
types.
The standard does not say anything about cin, only about std::cin.

>[snip]
>>But if I am right, how did this make it into such a high profile book
by such a high profile author...so I question my understanding.
Check the errate pages. Perhaps there's something.

It isn't there.
Too bad. Would you care to post a /complete/ code example? Then I (or
others) can say whether it's actually an error in the book (assuming
it's not an example of invalid code, discussed as such), or whether it's
a misinterpretati on or something on your part.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
4559
by: Steven T. Hatton | last post by:
Pete Vilder mentioned this a few days back. This is the "Compiler-Firewall Idiom", or pimple. It's similar to an approach taken in some Java designs. AAMOF, when I saw it in Java, my first reaction was "Oh by Ŝor! It's freakin' headers all over again!" My greatest misgiving about the approach is that it seems overly complicated. I /feel/ as if there is a better more 'conventional' way of dealing with the separation of interface and...
3
2368
by: Dave | last post by:
Hello all, I found Herb Sutter's article regarding error handling in the most recent CUJ to be very, very good. However, in trying to apply what I learned from the article, I am left with some questions. I have a function that takes a std::string (that represents a file name) as a parameter. This function reads in the contents of the file and returns a struct representing the file's content.
0
1397
by: ketan | last post by:
Hi, While understanding name lookup using item 32 of EC++ ( Sutter ), i found error from gcc bit unclear. It will be great if anyone can help me. In short - compiler complains about reference while there is no reference in signature. Code
7
14970
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following error/warning: "error: invalid use of nonstatic data member " However, he did NOT mention this error in the book explicitly.It happens always in the constructor when you try to initialize some data members in the constructor and try to accsess other data...
2
1225
by: puzzlecracker | last post by:
Why Attempt #3 is Deplorable First, let's consider a few reasons why Attempt #3 above truly is deplorable form: 1. Alignment. Unlike dynamic memory acquired by ::operator new(), the x_ character buffer isn't guaranteed to be properly aligned for objects of type X. To make this work more reliably, the programmer could use a "max_align" union, which is usually implemented as something like:
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.