g ++打破std :: filesystem :: last_write_time中的更改

Leś*_*ajs 4 c++ g++ c++17

在最近的OS升级中,我注意到我的代码中的一小部分停止了编译-看来原因是从切换g++-8g++-9

这段代码可以正常编译g++ 8.3.0(使用gcc:8.3Dockerhub的图像对此进行了验证)

#include <filesystem>
#include <chrono>

int main() {
    namespace fs = std::filesystem;
    using namespace std::chrono_literals;
    std::filesystem::last_write_time("test", std::chrono::system_clock::now() - 5min);
}
Run Code Online (Sandbox Code Playgroud)

g++ 9.1.0它失败与:

test.cpp: In function 'int main()':
test.cpp:8:69: error: no matching function for call to 'last_write_time(const char [5], std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >)'
    8 |  fs::last_write_time("test", std::chrono::system_clock::now() - 5min);
      |                                                                     ^
In file included from /usr/local/include/c++/9.1.0/filesystem:39,
                 from test.cpp:1:
/usr/local/include/c++/9.1.0/bits/fs_ops.h:243:19: note: candidate: 'std::filesystem::file_time_type std::filesystem::last_write_time(const std::filesystem::__cxx11::path&)'
  243 |   file_time_type  last_write_time(const path& __p);
      |                   ^~~~~~~~~~~~~~~
/usr/local/include/c++/9.1.0/bits/fs_ops.h:243:19: note:   candidate expects 1 argument, 2 provided
/usr/local/include/c++/9.1.0/bits/fs_ops.h:244:19: note: candidate: 'std::filesystem::file_time_type std::filesystem::last_write_time(const std::filesystem::__cxx11::path&, std::error_code&)'
  244 |   file_time_type  last_write_time(const path& __p, error_code& __ec) noexcept;
      |                   ^~~~~~~~~~~~~~~
In file included from /usr/local/include/c++/9.1.0/filesystem:36,
                 from test.cpp:1:
/usr/local/include/c++/9.1.0/bits/fs_fwd.h:362:47: note:   no known conversion for argument 2 from 'std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >' to 'std::error_code&'
  362 |   file_time_type last_write_time(const path&, error_code&) noexcept;
      |                                               ^~~~~~~~~~~
In file included from /usr/local/include/c++/9.1.0/filesystem:39,
                 from test.cpp:1:
/usr/local/include/c++/9.1.0/bits/fs_ops.h:245:8: note: candidate: 'void std::filesystem::last_write_time(const std::filesystem::__cxx11::path&, std::filesystem::file_time_type)'
  245 |   void last_write_time(const path& __p, file_time_type __new_time);
      |        ^~~~~~~~~~~~~~~
/usr/local/include/c++/9.1.0/bits/fs_ops.h:245:56: note:   no known conversion for argument 2 from 'time_point<std::chrono::_V2::system_clock,[...]>' to 'time_point<std::filesystem::__file_clock,[...]>'
  245 |   void last_write_time(const path& __p, file_time_type __new_time);
      |                                         ~~~~~~~~~~~~~~~^~~~~~~~~~
/usr/local/include/c++/9.1.0/bits/fs_ops.h:246:8: note: candidate: 'void std::filesystem::last_write_time(const std::filesystem::__cxx11::path&, std::filesystem::file_time_type, std::error_code&)'
  246 |   void last_write_time(const path& __p, file_time_type __new_time,
      |        ^~~~~~~~~~~~~~~
/usr/local/include/c++/9.1.0/bits/fs_ops.h:246:8: note:   candidate expects 3 arguments, 2 provided

shell returned 1

Press ENTER or type command to continue
Run Code Online (Sandbox Code Playgroud)

编译命令:(g++ -std=c++17 test.cpp -lstdc++-fs尽管stdc++-fs自以来不需要链接g++9

我的问题是-此功能的惯用用法是什么?即将文件的上次写入时间更改为五分钟前。

我了解,如果引入了重大更改,我会以某种非惯用的方式使用它。

Nat*_*ica 8

正如@LightnessRacesinOrbit在他们的答案中指出的那样,std::filesystem::file_time_typelast_write_time方法使用了未指定的time_point类型。这意味着,从一个编译器转移到同一编译器的另一个甚至版本时,完全合法。

但是,您可以做的是获取实现使用的时钟,然后自己使用。 std::chrono::time_point旨在将创建它的时钟类型作为模板参数,并且它浮现了一个clock代表它的公共类型。因此,为了方便地获取时钟并调用now它,您可以使用

std::filesystem::last_write_time("test", std::filesystem::file_time_type::clock::now() - 5min);
//                                       ^ give me whatever clock you use and call now on it
Run Code Online (Sandbox Code Playgroud)