首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用<format>头

如何使用<format>头
EN

Stack Overflow用户
提问于 2020-04-26 13:19:35
回答 3查看 8.6K关注 0票数 20

在一个相关的问题("https://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf")中,我了解了这个可怕的新C++20头//en.cppreference.com/w/cpp/utility/format/format

然而,似乎没有支持编译器.这是正确的,还是有办法使用它?

我使用的是g++ 9.3和-std=c++2a标志,而库<format>是不被识别的。

代码语言:javascript
复制
#include <format> // fatal error: format: No such file or directory
#include <iostream>

int main(){
    std::cout << std::format("Hello {}!", "World");
}

g++-9 test.cpp -o test -std=c++2a

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-04-26 13:32:32

使用libfmt<format>头实质上是一个标准化的libfmt (如果我没记错的话,它删除了一些小功能)。

票数 15
EN

Stack Overflow用户

发布于 2021-04-07 15:24:21

debian/ubuntu上有一个libfmt-dev包可用。

我目前使用这个头作为format头的替代:

代码语言:javascript
复制
#include <fmt/core.h>
#include <fmt/ranges.h>

namespace std
{
   template<typename... Args>
   inline auto format(Args&&... args) -> decltype(fmt::v7::format(std::forward<Args>(args)...))
   {
      return fmt::v7::format(std::forward<Args>(args)...);
   }
}

这是实验性的:我不知道std::formatfmt::format到底有什么不同。

生活充满了惊喜

票数 4
EN

Stack Overflow用户

发布于 2022-10-03 01:03:36

在编写本报告时,MSVCClang是仅有的两个支持标头的编译器。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61441494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档