I have a function that works under C++14 using the date.h library but I'm converting my program to use C++20 and it's no longer working. What am I doing wrong, please?
My C++14/date.h code is as follows:
#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);
}
My converted C++20 function is as follows:
#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);
}
The error I'm receiving in VS2019 Community (latest) is:
E0304 no instance of overloaded function "parse" matches the argument list
Are there any subtle changes that have been made and I'm missing? What could be causing this error, please?
There's a bug in the spec that is in the process of being fixed. And VS2019 faithfully reproduced the spec. Wrap your format string in string{}
, or give it a trailing s
literal to turn it into a string, and this will work around the bug.
in >> parse("%Y-%m-%d %H:%M:%S"s, tp);