小编Sco*_*ykl的帖子

我可以使用包含语法的 const char* 或 std::string 变量作为 libfmt 的参数吗?

希望这是一个愚蠢的问题。我有以下代码:

#include <iostream>
#include <fmt/format.h>
#include <string>
int main(){
 double f = 1.23456789;
 std::cout << fmt::format( "Hello {:f} how are you?\n", f ) << "\n";
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

这按预期工作 -你好 1.234568 你好吗?

但是,如果我想将传递到 fmt::format 的字符串封装为变量,则会遇到编译器错误:

#include <iostream>
#include <fmt/format.h>
#include <string>
int main() {
 double f = 1.23456789;
 const char* m = "Hello {:f} how are you?\n"; //can't be constexpr, generated at run time
 std::cout << fmt::format( m, f ) << "\n";
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,在 MSVC 2022 上使用 …

c++ format c++20 fmt

3
推荐指数
1
解决办法
1242
查看次数

标签 统计

c++ ×1

c++20 ×1

fmt ×1

format ×1