You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何std::println输出std::vector会编译失败?

格式化std::vector时编译失败的问题

问题描述

编写如下代码:

#include <print>
#include <vector>

int main() {
    std::vector<int> v{1, 2, 3};
    std::println("{}", v);
}

使用clang++ -std=c++23 -stdlib=libc++编译时,出现错误:

[...]/format_arg_store.h:167:17: error: static assertion failed due to [...]
167 | static_assert(__arg != __arg_t::__none, "the supplied type is not formattable");
| ^~~~~~~~~~~~~~~~~~~~~~~~

已知libc++支持std::println且实现了P2286R8(格式化范围),疑惑此错误的原因是代码问题还是标准库bug。

解决方法

问题根源是缺少必要的头文件:格式化范围(如std::vector这类容器)的支持逻辑定义在<format>头文件中,而<print>并未自动引入该头文件。只需在代码中添加<format>的包含即可解决:

#include <print>
#include <format>
#include <vector>

int main() {
    std::vector<int> v{1, 2, 3};
    std::println("{}", v);
}

另外需确认clang/libc++版本足够新——只有clang 16及以上版本才完整实现了P2286R8的格式化范围支持,旧版本即使指定-std=c++23也可能无法正常工作。

内容的提问来源于stack exchange,提问作者Jan Schultke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 22:29:56