6

我试图使用 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

4

1 回答 1

12

据此: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

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

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

于 2020-12-01T01:46:05.330 回答