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

2024-02-24

我试图使用 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 https://en.cppreference.com/w/cpp/compiler_support目前没有编译器支持“文本格式”(P0645R10 https://wg21.link/P0645R10, std::format)。 (截至2020年12月)

该论文定义的功能测试宏是__cpp_lib_format(还列出了here https://en.cppreference.com/w/cpp/feature_test),所以你可以像这样编写代码来检查:

#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 https://github.com/fmtlib/fmt作为一个完整的实施,fmt::format代替std::format。尽管您必须跳过一些步骤来链接依赖项或将其添加到您的构建系统中,并在必要时处理许可证/确认。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐