sentry member class

J

john

I am reading TC++PL3 and in 21.3.8 it describes the class sentry, which
is member of basic_istream and basic_ostream.

Q1: Should we always use the sentry class when we define new functions
and operators for basic_ostream/basic_istream (ostream/istream)?

Q2: It is mentioned that we can place code "such as throwing exceptions
caused by state changes" in the sentry's destructor. However in 14.4.7
it is mentioned:

"Exiting from a destructor by throwing an exception is also a violation
of the standard library requirements (E2)".

I suppose both sentry's constructor and destructor, are to be provided
by the implementer of the standard library basic_istream and
basic_ostream classes?
 
J

James Kanze

I am reading TC++PL3 and in 21.3.8 it describes the class sentry, which
is member of basic_istream and basic_ostream.
Q1: Should we always use the sentry class when we define new functions
and operators for basic_ostream/basic_istream (ostream/istream)?

It depends. If all your operator does is call existing <</>>
operators, it probably isn't necessary; those operators will
themselves create an instance of the sentry class. But in
general, yes.
Q2: It is mentioned that we can place code "such as throwing exceptions
caused by state changes" in the sentry's destructor.

I suspect that you've misquoted something here. You can't place
anything in sentry's destructor, since it is provided by the
standard library, and you cannot modify it.
However in 14.4.7
it is mentioned:
"Exiting from a destructor by throwing an exception is also a violation
of the standard library requirements (E2)".
I suppose both sentry's constructor and destructor, are to be provided
by the implementer of the standard library basic_istream and
basic_ostream classes?

Exactly.
 

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,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top