在 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)
代码