lundi 12 avril 2021

What is the correct behavior of std::get_time() for "short" input

I'm trying to understand what should be the correct behavior of C++11 std::get_time() when the input data is "shorter" than expected by the format string. For example, what the following program should print:

#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;
}

Note that get_time() behavior is described in terms of std::time_get<CharT,InputIt>::get(). Based on the latter (see 1c paragraph) I would expect both eofbit and failbit to be set and so the program to print:

eof:  1
fail: 1

However, for all the major Standard C++ Library implementations (libstdc++ 10.2.1, libc++ 11.0.0, and MSVC 16.8) it prints:

eof:  1
fail: 0

Interestingly, that for MSVC before 16.8 it prints:

eof:  1
fail: 1

But the commit "std::get_time should not fail when format is longer than the stream" suggests that this was fixed deliberately.

Could someone clarify if (and why) the mentioned standard libraries behave correctly and, if that's the case, how it is supposed to detect that the format string was not fully used.

Aucun commentaire:

Enregistrer un commentaire