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

为何std::make_format_args要求非const引用?

关于std::make_format_args接受非const引用的疑问及代码报错问题

我查阅cppreference文档后,无法理解为何std::make_format_args()接受引用而非const引用?

我想要实现如下代码:

#include <format>

template <class... Args>
inline static std::string format(const std::string_view field, Args&&... args)
{ 
    return std::vformat(field, std::make_format_args(std::forward<Args>(args)...)); 
}

我希望能在args中传入const std::string&,但std::make_format_args()似乎要求非const引用,因此出现如下错误:

A non-const reference may only be bound to an lvalue C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\format(3713): note: see declaration of 'std::make_format_args'
note: while trying to match the argument list '(_Ty, const std::string, _Ty, _Ty)'

更新
可通过在线编译器复现该错误,本地报错与上述一致


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 15:42:06