格式化没有这样的文件或目录

cae*_*ras 4 c++ gcc fmt

我试图使用 C++ 格式实用程序 (std::format)。我试图编译这个简单的程序:

#include <format>

int main()
{
   std::cout << std::format("{}, {}", "Hello world", 123) << std::endl;

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试用 编译时g++ -std=c++2a format_test.cpp,它给了我这个:

format_test.cpp:1:10: fatal error: format: No such file or directory
    1 | #include <format>
      |
Run Code Online (Sandbox Code Playgroud)

我有 GCC 10.2.0

fra*_*ike 12

文本格式化 P0645R10是在

  • GCC libstdc++ 13
  • Clang libc++ 14(但标记为不完整)
  • MSVC STL 19.29(VS 2019 16.10)

来源


Art*_*yer 8

根据这个:https : //en.cppreference.com/w/cpp/compiler_support目前没有支持“文本格式”(P0645R10std::format)的编译器。(截至 2020 年 12 月)

该论文定义的功能测试宏是__cpp_lib_format (也在此处列出),因此您可以编写这样的代码来检查:

#if __has_include(<format>)
#include <format>
#endif

#ifdef __cpp_lib_format
// Code with std::format
#else
// Code without std::format, or just #error if you only
// want to support compilers and standard libraries with std::format
#endif
Run Code Online (Sandbox Code Playgroud)

该提案还链接到https://github.com/fmtlib/fmt作为完整实现,fmt::format而不是std::format. 尽管您必须跳过一些麻烦来链接依赖项或将其添加到您的构建系统,并在必要时处理许可/确认。

你的例子{fmt}https : //godbolt.org/z/Ycd7K5

  • 都2022年了,这还没有实施吗? (10认同)
  • @FreelanceConsultant:如果它还没有实现,那么为什么在书籍和参考文献中提到它?我在 https://en.cppreference.com/w/cpp/utility/format/format 上阅读了有关此库的信息。即使我运行示例代码片段,我也会遇到与上面相同的错误。我很困惑为什么文档中大量提到尚未实现的功能。 (5认同)
  • @Avinash 可能是因为文献领先于实际开发。标准委员会首先发布他们的标准文档,然后编写文献并开始针对这些新功能的编译器开发。这是我对发生的事情的猜测 (2认同)

Pav*_*ath 1

终于,std::format现在gcc 13.1版本支持了! https://godbolt.org/z/TW9G8Gh5Y