xml*_*lmx 5 c++ standards type-safety c++20 compile-time-type-checking
cppref页面上std::format说:
提供多于格式字符串所需的参数并不是错误:
// OK, produces "Hello world!"
std::format("{} {}!", "Hello", "world", "something");
Run Code Online (Sandbox Code Playgroud)
既然std::format有一个编译时检查来查看fmt参数是否不匹配,为什么上面的示例代码不被视为错误呢?
背后的原理是什么?
n. *_* m. 12
有几个原因。
您可以在格式字符串中多次使用相同的参数:
std::fomat("{1},{0},{1}", x, y);
Run Code Online (Sandbox Code Playgroud)
没有理由排除零次提及它。
它实际上在提供本地化字符串方面很方便:
std::vformat(get_string("Preheat the oven to {} degrees"), temp, temp*9/5+32);
Run Code Online (Sandbox Code Playgroud)
返回的字符串get_string将包含{0}或{1}。
请注意,这与是否进行编译时或运行时检查无关。无论何时执行哪些检查,它(应该)要么被视为错误,要么不被视为错误。
| 归档时间: |
|
| 查看次数: |
179 次 |
| 最近记录: |