Old*_*ier 9 c++ gcc iostream clang manipulators
应该使用std :: ws操纵器从流中提取是否会引发失败位?在以下代码中,Clang编译(在Xcode 4.5.1中)程序未通过最终断言.显然s >> std::ws,EOF会导致失败.然而,GCC 4.7.2通过了断言.哪个是对的?
#include <iostream>
#include <sstream>
#include <cassert>
int main(int argc, const char * argv[])
{
{
// Read string with trailing ws.
std::istringstream s( "test " );
std::string test;
s >> std::ws;
assert( !s.fail() ); // No ws to skip, but no failure.
s >> test;
assert( test == "test" );
assert( !s.fail() );
s >> std::ws;
assert( !s.fail() ); // No prob skipping trailing ws.
}
{
// Retry with no trailing ws.
std::istringstream s( "test" );
std::string test;
s >> std::ws;
assert( !s.fail() ); // No ws to skip, but no failure.
s >> test;
assert( test == "test" );
assert( !s.fail() );
s >> std::ws;
assert( !s.fail() ); // CLANG: Skipping absent ws at eof raises failbit.
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C++11,\xc2\xa727.7.2.4/1:
\n\n\n\n\n如果
\nws停止提取字符,因为它设置的不再可用eofbit,但不是failbit。
因此,ws操纵器并不failbit直接设置。然而,正如 Marshall Clow 在他的回答中指出的那样,它不必这样做——它需要创建一个哨兵对象,并且哨兵对象需要设置失败位 if !stream.good()。
| 归档时间: |
|
| 查看次数: |
494 次 |
| 最近记录: |