By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,859 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,859 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.