【问题标题】:C++20 chrono parse problem in VS2019 (latest)VS2019 中的 C++20 chrono 解析问题(最新)
【发布时间】:2021-08-26 13:44:57
【问题描述】:

我有一个使用 date.h 库在 C++14 下工作的函数,但我正在将我的程序转换为使用 C++20,但它不再工作。请问我做错了什么?

我的 C++14/date.h 代码如下:

#include <date/date.h> // latest, installed via vcpkg
#include <chrono>

auto StringToUnix(const std::string& source) -> std::time_t
{
    auto in = std::istringstream(source);
    auto tp = date::sys_seconds{};
    in >> date::parse("%Y-%m-%d %H:%M:%S", tp); 

    return std::chrono::system_clock::to_time_t(tp);
}

我转换后的C++20函数如下:

#include <chrono>

auto StringToUnix(const std::string& source) -> std::time_t
{
    using namespace std::chrono;
    auto in = std::istringstream(source);
    auto tp = sys_seconds{};
    in >> parse("%Y-%m-%d %H:%M:%S", tp);

    return system_clock::to_time_t(tp);
}

我在 VS2019 社区(最新)中收到的错误是:

E0304   no instance of overloaded function "parse" matches the argument list    

是否有任何细微的变化而我错过了?请问这个错误是什么原因造成的?

【问题讨论】:

  • 您的编译器是否支持新的日期功能?查看编译器的文档,了解它支持多少 C++20,因为目前还没有编译器提供 100% 的支持。

标签: c++ date c++20 chrono


【解决方案1】:

规范中有一个错误正在修复中。并且 VS2019 忠实地再现了规范。将格式字符串包装在 string{} 中,或者给它一个尾随的 s 文字以将其转换为字符串,这将解决该错误。

in >> parse("%Y-%m-%d %H:%M:%S"s, tp);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2021-11-08
    • 2013-05-18
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多