473,385 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to use ostream::sentry?


#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::operator bool() [with _CharT =
char, _Traits = std::char_traits<char>]' discards qualifiers

Who can tell me why?
--
---------------snnn-------------
http://snnn.blogone.net
Jul 22 '05 #1
5 4682
snnn wrote:

#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::operator 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.
Jul 22 '05 #2
??
The codes above can work well under VC++2003,but not gcc!

Jul 22 '05 #3
Oh,i know,i need to remove "const"

Jul 22 '05 #4
Ron Natalie wrote:
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::operator 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).
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #5
Dietmar Kuehl wrote:
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.
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).


Yes, whatever compiler that error came from.
Jul 22 '05 #6

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

Similar topics

12
by: Minti | last post by:
Is std::cout slower than printf When we call printf e.g. in printf(20 format conversion specifications, 20 arguments); Is it faster than the std::cout << { 20 redirections to the output...
8
by: Markus Dehmann | last post by:
My ios::uppercase (and others) seems not to work. #include <iostream> using namespace std; int main(){ int num = 45; cout.setf(ios::hex | ios::showbase | ios::uppercase); cout << "number " <<...
6
by: Marco Chiarandini | last post by:
Dear all, I am experiencing a problem in the deallocation of STL data structures. In paritcular I create a vector of sets and insert integers in each set. I do not define any deallocator since...
7
by: S. Nurbe | last post by:
Hi, probably this is a common problem but I couldn't find yet a proper solution (and I hope there is one). I have two relative complex frameworks: one uses only iostream.h the other one only...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
7
by: Peter Jansson | last post by:
Dear group, I have been struggling to get a simple program for inserting and extracting std::tm objects to/from streams to work. The code below tries to read a std::tm object from a...
8
by: jean.daniel.michaud | last post by:
Hi all, Something I don't get. The code is: // snippet on #include <list> #include <iostream> int main()
1
by: john | last post by:
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...
4
by: subramanian100in | last post by:
In the book, C++ Coding Standards book by Hereb Sutter and Andrei Alexandrescu, in Item 40 on pages 86-87 viz, "Avoid providing implicit conversions", the authors have advised the use of named...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.