How to use ostream::sentry?

S

snnn

#include <iostream>

using namespace std;
int main(){
const ostream::sentry ok(cout);
if(ok) return 0;
else return -1;
}

The codes above can work well under VC++2003,but not gcc!
When I try to use gcc to compile it ,the error I get
main.cpp: In function `int main()':
main.cpp:37: error: passing `const std::basic_ostream<char,
std::char_traits<char> >::sentry' as `this' argument of `
std::basic_ostream<_CharT, _Traits>::sentry::eek:perator bool() [with _CharT =
char, _Traits = std::char_traits<char>]' discards qualifiers

Who can tell me why?
 
R

Ron Natalie

snnn said:
#include <iostream>

using namespace std;
int main(){
const ostream::sentry ok(cout);
if(ok) return 0;
else return -1;
}

The codes above can work well under VC++2003,but not gcc!
When I try to use gcc to compile it ,the error I get
main.cpp: In function `int main()':
main.cpp:37: error: passing `const std::basic_ostream<char,
std::char_traits<char> >::sentry' as `this' argument of `
std::basic_ostream<_CharT, _Traits>::sentry::eek:perator bool() [with _CharT =
char, _Traits = std::char_traits<char>]' discards qualifiers

Who can tell me why?

What good is a const sentry?

Anyhow, looks like VC++ doesn't declare the operator bool() as const.
 
D

Dietmar Kuehl

Ron said:
When I try to use gcc to compile it ,the error I get | ^^^
main.cpp: In function `int main()':
main.cpp:37: error: passing `const std::basic_ostream<char,
std::char_traits<char> >::sentry' as `this' argument of `
std::basic_ostream<_CharT, _Traits>::sentry::eek:perator bool() [with _CharT =
char, _Traits = std::char_traits<char>]' discards qualifiers
What good is a const sentry?

The 'sentry's operations are done on construction and on destruction.
In
the remainder of its life-time it does not change and can thus be
'const':
nothing wrong with this.
Anyhow, looks like VC++ doesn't declare the operator bool() as const.

No, it is gcc which doesn't declare the 'operator bool()' as 'const',
at
least if the error message is genuine. That's a plain error as the
operator
is supposed to be 'const' according to 27.6.2.3 (lib.ostream::sentry).
 
R

Ron Natalie

Dietmar said:
The 'sentry's operations are done on construction and on destruction.
In
the remainder of its life-time it does not change and can thus be
'const':
nothing wrong with this.

Perhaps I phrased that wrong. You're right, but there's nothing really
to be gained by making the sentry const since nothing touches it.
No, it is gcc which doesn't declare the 'operator bool()' as 'const',
at
least if the error message is genuine. That's a plain error as the
operator
is supposed to be 'const' according to 27.6.2.3 (lib.ostream::sentry).

Yes, whatever compiler that error came from.
 

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

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top