小编Bam*_*ghh的帖子

是否可以像 python 的 f-strings 那样在 C++ 中格式化字符串?

在 Python 中,可以使用 f-strings 方便地格式化字符串:

a = 42
print(f"a = {a}") # prints "a = 42"
Run Code Online (Sandbox Code Playgroud)

是否可以在编译时在 C++ 中做这样的事情,或者是format("a = {}", a);最接近 f-strings 的东西?

编辑:我并不是说这些格式化方法应该在运行时工作。编译器不能只在编译时通过名称查找当前作用域中可见的变量吗?就像,如果它遇到字符串,f"foo = {foo}"它可以将此字符串替换为std::format("foo = {}", foo)代码

c++ python f-string

12
推荐指数
3
解决办法
3598
查看次数

标签 统计

c++ ×1

f-string ×1

python ×1