是否为 get_time 定义了重复赋值?

Is Repeated Assignment Defined for get_time?

我正在研究 this answer. And I ran into a conundrum: scanf has an assignment suppressing '*':

If this option is present, the function does not assign the result of the conversion to any receiving argument

但是当在 get_time 中使用时,'*' 在 Visual Studio、libc++ 和 libstdc++ 上给出了 运行 时间错误:str >> get_time(&tmbuf, "%T.%*Y") 所以我相信不支持。

因此,我选择通过读取 tmbuf.tm_year 两次来忽略输入:

str >> get_time(&tmbuf, "%H:%M:%S.%Y UTC %b %d %Y");

This worksget_time 而言似乎是我唯一的选择,因为 '*' 未被接受。但众所周知,仅仅因为它有效并不意味着它已被定义。有人可以确认:

  1. get_time
  2. 中定义了对同一个变量赋值两次
  3. 流将始终从左到右读取,因此 %Y 的第 1st 事件将被踩踏,而不是第 2nd

标准规定了在22.4.5.1.1time_get成员中处理get_time格式字符串的具体算法。 (time_get::get 是您执行 str>>get_time(...) 时最终调用的内容)。我引用重要部分:

The function starts by evaluating err = ios_base::goodbit. It then enters a loop, reading zero or more characters from s at each iteration. Unless otherwise specified below, the loop terminates when the first of the following conditions holds:

(8.1) — The expression fmt == fmtend evaluates to true.

skip boring error-handling parts

(8.4) — The next element of fmt is equal to ’%’, optionally followed by a modifier character, followed by a conversion specifier character, format, together forming a conversion specification valid for the ISO/IEC 9945 function strptime. skip boring error-handling parts the function evaluates s = do_get(s, end, f, err, t, format, modifier) skip more boring error-handling parts the function increments fmt to point just past the end of the conversion specification and continues looping.

从描述中可以看出,格式化字符串严格按照从左到右的顺序进行处理。没有专门处理重复转换规范的规定。所以答案肯定是肯定的,你所做的是定义明确且完全合法的。