【发布时间】:2023-03-14 21:57:01
【问题描述】:
我试图了解 C++11 的正确行为应该是什么 std::get_time() 当输入数据比格式预期的“短”时 细绳。例如,下面的程序应该打印什么:
#include <ctime>
#include <iomanip>
#include <sstream>
#include <iostream>
int main (int argc, char* argv[])
{
using namespace std;
tm t {};
istringstream is ("2016");
is >> get_time (&t, "%Y %d");
cout << "eof: " << is.eof () << endl
<< "fail: " << is.fail () << endl;
}
注意get_time() 行为被描述为 std::time_get<CharT,InputIt>::get()。 基于后者(见 1c 段),我希望 eofbit 和 failbit 要设置和打印的程序:
eof: 1
fail: 1
但是,对于所有主要的标准 C++ 库实现(libstdc++ 10.2.1、libc++ 11.0.0 和 MSVC 16.8)它打印:
eof: 1
fail: 0
有趣的是,对于 16.8 之前的 MSVC,它会打印:
eof: 1
fail: 1
但是提交"std::get_time should not fail when format is longer than the stream" 表明这是故意修复的。
有人可以澄清上述标准库是否(以及为什么)行为正确,如果是这样,它应该如何检测格式字符串未完全使用。
【问题讨论】:
标签: c++11 std date-parsing