【发布时间】:2019-10-28 04:37:12
【问题描述】:
这个最小程序使用boost::program_options 来解析stringstream。奇怪的是,解析后,流不再处于“良好”状态,并且设置了failbit和eofbit。
#include <iostream>
#include <sstream>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
void test_stream(std::stringstream& s);
int main()
{
using namespace std;
namespace po = boost::program_options;
stringstream s;
s << "seed=3" << '\n';
test_stream(s);
po::options_description desc("");
desc.add_options()
("seed", po::value<int>());
po::variables_map vm;
po::store(po::parse_config_file(s, desc, true), vm);
po::notify(vm);
test_stream(s);
return 0;
}
void test_stream(std::stringstream& s)
{
using namespace std;
if (s.good())
{
cout << "stream is good" << endl;
}
else
{
cout << "stream is not good" << endl;
if (s.rdstate() & ios_base::badbit)
cout << "badbit is set" << endl;
if (s.rdstate() & ios_base::failbit)
cout << "failbit is set" << endl;
if (s.rdstate() & ios_base::eofbit)
cout << "eofbit is set" << endl;
}
}
输出:
stream is good
stream is not good
failbit is set
eofbit is set
虽然 eof 条件在某种程度上是预期的,但由于推测解析器在 EOF 之前已经读取了流,为什么还要设置故障位?
【问题讨论】:
标签: c++ stream boost-program-options