问题描述
以下(玩具)程序在链接到libstdc ++和libc ++时会返回不同的内容。这是libc ++中的一个错误,或者我不明白istream eof()如何工作?我试过运行它使用g ++在linux和mac os x和clang在mac os x,有和没有-std = c ++ 0x。这是我的印象,eof()不返回true,直到尝试读取(通过get()或其他)实际上失败。这是libstdc ++的行为,但不是libc ++的行为。
The following (toy) program returns different things when linked against libstdc++ and libc++. Is this a bug in libc++ or do I not understand how istream eof() works? I have tried running it using g++ on linux and mac os x and clang on mac os x, with and without -std=c++0x. It was my impression that eof() does not return true until an attempt to read (by get() or something else) actually fails. This is how libstdc++ behaves, but not how libc++ behaves.
#include <iostream>
#include <sstream>
int main() {
std::stringstream s;
s << "a";
std::cout << "EOF? " << (s.eof() ? "T" : "F") << std::endl;
std::cout << "get: " << s.get() << std::endl;
std::cout << "EOF? " << (s.eof() ? "T" : "F") << std::endl;
return 0;
}
Thor:~$ g++ test.cpp
Thor:~$ ./a.out
EOF? F
get: 97
EOF? F
Thor:~$ clang++ -std=c++0x -stdlib=libstdc++ test.cpp
Thor:~$ ./a.out
EOF? F
get: 97
EOF? F
Thor:~$ clang++ -std=c++0x -stdlib=libc++ test.cpp
Thor:~$ ./a.out
EOF? F
get: 97
EOF? T
Thor:~$ clang++ -stdlib=libc++ test.cpp
Thor:~$ ./a.out
EOF? F
get: 97
EOF? T
推荐答案
这是一个libc ++错误, Cubbi指出。我的错。详情如下:
This was a libc++ bug and has been fixed as Cubbi noted. My bad. Details are here:
这篇关于istream eof libc ++和libstdc ++之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!