473,385 Members | 2,044 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.

is input_stream >> std::ws supposed to set fail() bit?

Hi All,

Consider the following block of code:

std::istringstream in( "1" ); // no space after `1'
int i;
in >i >std::ws;
assert( !in::fail() )

Does the standard have any mention whether assertion fails or not?
Note that, with gcc and msvc assertion doesn't fail, but there is
compiler which it does (portland group C++) .

thanks many
slyi

Sep 19 '07 #1
1 2932
On Sep 20, 1:06 am, levent <leventyil...@gmail.comwrote:
Consider the following block of code:
std::istringstream in( "1" ); // no space after `1'
int i;
in >i >std::ws;
assert( !in::fail() )
Does the standard have any mention whether assertion fails or not?
I may not fail. The read of i succeeds, and manipulators never
set failbit.
Note that, with gcc and msvc assertion doesn't fail, but there is
compiler which it does (portland group C++) .
The STL port (at least the version which comes with Sun CC) has
the same bug. If you have to be portable to such an
implementation, just write your own, and use it:

std::istream&
ws( std::istream& source )
{
if ( source.good() ) {
std::ctype< char const&
ctype = getFacet( source.getloc() ) ;
std::streambuf* sb = source.rdbuf() ;
int c = sb->sgetc() ;
while ( c != EOF
&& ctype.is( std::ctype_base::space,
static_cast< char >( c ) ) ) {
c = sb->snextc() ;
}
if ( c == EOF ) {
source.setstate( std::ios::eofbit ) ;
}
}
return source ;
}

Obviously, this is in my own namespace, so I write Gabi::ws,
rather than std::ws. And while it's not a template in my case
(I don't use anything but the char based streams), it wouldn't
be too hard to make it one.

Also, Gabi::getFacet is used, rather than std::use_facet, as a
work-around to problems in the default Sun CC library:

namespace GetFacetPrivate {

class FacetGetter ;
class FacetAddress
{
friend class FacetGetter ;
public:
template< typename Facet >
operator Facet const*() const
{
return &static_cast< Facet const& >( *myOwner ) ;
}

private: // Except for FacetGetter...
explicit FacetAddress( FacetGetter const* owner )
: myOwner( owner )
{
}

private: // Really...
FacetGetter const* myOwner ;
} ;

class FacetGetter
{
public:
explicit FacetGetter( std::locale const& locale )
: myLocale( &locale )
{
}

FacetAddress operator&() const
{
return FacetAddress( this ) ;
}

operator FacetGetter const&() const
{
return *this ;
}

template< typename Facet >
operator Facet const&() const
{
return get( *myLocale, &std::use_facet< Facet ) ;
}

private:
std::locale const* myLocale ;

template< typename Facet >
Facet const& get( std::locale const& locale,
Facet const& (*f)( std::locale
const& ) ) const
{
return (*f)( locale ) ;
}

template< typename Facet >
Facet const& get( std::locale const& locale,
Facet const& (*f)( std::locale
const&,
Facet* ) ) const
{
return (*f)( locale, NULL ) ;
}
} ;
}

inline GetFacetPrivate::FacetGetter
getFacet(
std::locale const& locale )
{
return GetFacetPrivate::FacetGetter( locale ) ;
}

As written, it has the added advantage of not requiring you to
specify the type if it is being used to directly initialize a
reference (which is almost always the case).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 20 '07 #2

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

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
3
by: Juicer_X | last post by:
Hello everyone, I've been working with the STL Containers for a little while now, but in the middle of working on a small "Markov Chain" class I realized that I wanted to modify my frequency...
4
by: Siemel Naran | last post by:
My compiler (Borland C++) fails to compile this code: ifstream file(filename.c_str()); ostringstream out; file >> out.rdbuf(); with the error in the 3rd line above that: realmain.cpp(83):...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
9
by: alopatenko | last post by:
I have a template class template <Class W> class WS At some point I have to use a STL list WS<W>objects so, I define #include <list>
5
by: Keith | last post by:
Hello all, I have a C# Windows Forms app. It is in namespace App.GUI. It builds to Nav.exe. I have entered an application level setting using the designer. Its type is string, name is "FOO"...
9
by: Notebooker | last post by:
Hello, I'm an intermediate noob reading-in data from ascii-file using an ifstream object. I have specified a c-style string buffer with size of type size_t and I am specifying to use this...
13
by: liujiaping | last post by:
Hi, all. I have a dictionary-like file which has the following format: first 4 column 7 is 9 a 23 word 134 .... Every line has two columns....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.