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

Format no such file or directory

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

#include <format>

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

   return 0;
}

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

format_test.cpp:1:10: fatal error: format: No such file or directory
    1 | #include <format>
      |

我有 GCC 10.2.0

据此:https://en.cppreference.com/w/cpp/compiler_support there are currently no compilers that support "Text formatting" (P0645R10std::format)。 (截至 2020 年 12 月)

那篇论文定义的特征测试宏是__cpp_lib_format(也列出here),所以你可以这样写你的代码来检查:

#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

提案还 link 提交给 https://github.com/fmtlib/fmt 作为完整实施,使用 fmt::format 而不是 std::format。尽管您必须跳过一些步骤才能 link 依赖项或将其添加到您的构建系统并在必要时处理许可/确认。

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