英文:
fmt compile time format string check without generating asm code for the check?
问题
The user provided a code-related question with multiple parts. Here's the translated response for the relevant part:
在不生成任何汇编代码(最终不会被编译进去)的情况下,是否有办法要求 fmt
库对格式字符串与参数类型进行编译时检查?
对于示例,可以看到以下代码:
#include <fmt/core.h>
#include <fmt/compile.h>
int main() {
fmt::format(FMT_STRING("{:d}"), 123);
}
上面的代码会进行检查(如果编译器支持 constval
,FMT_STRING
是可选的),但它也会生成一些汇编代码,你可以在链接中看到。
我正在寻找一种方法,在编译时触发检查,而不会在最终的二进制文件中生成任何汇编代码。原因是我想创建自己的记录器(在 fmt
之上)(类似于这个),但我想推迟实际的格式化到另一个线程,只在编译时执行检查(没有任何汇编代码)。这是否可能?
英文:
Is there anyway to ask fmt
library to do compile time check on the format string against the argument types without generating any asm code (to be compiled out)?
For example,
#include <fmt/core.h>
#include <fmt/compile.h>
int main() {
fmt::format(FMT_STRING("{:d}"), 123);
}
Code above does the check (FMT_STRING
is optional for compiler supporting constval
) but it also generates some asm code as you can see in the link.
I am looking for a way to trigger the check at compile time without any asm in the final binary. The reason is that I would like to create my own logger (on top of fmt
) (similar idea to this) but I would like to defer the actual formatting to another thread later but only have the compile time check done (without any asm code). Is it possible?
答案1
得分: 3
使用 static_assert
检查 formatted_size
的返回值与 FMT_COMPILE
宏。
英文:
Use static_assert
to check the return value of formatted_size
with FMT_COMPILE
macro
static_assert(fmt::formatted_size(FMT_COMPILE("{:d}"), 123));
答案2
得分: 1
以下是翻译好的部分:
你可以按以下方式检查格式字符串:
#include <fmt/core.h>
int main() {
constexpr auto s = fmt::format_string<int>("{:d}");
}
这没有运行时效果,如你可以从生成的汇编中看到 (https://godbolt.org/z/Eqxa1hf5T):
main:
mov eax, 0
ret
通常情况下,format_string
用作函数参数,因此在调用函数时会自动执行检查,例如:
template <typename... Args>
void log(fmt::format_string<Args...> fmt, Args&&... args) {
// 在这一点上,格式字符串已经经过检查。
// 进行日志记录。
}
FMT_STRING
是一种用于进行编译时检查的传统机制,我不建议使用它。
我也不建议使用 FMT_COMPILE
进行检查,因为并不是所有的格式化器都能在编译时工作,并且它的效率较低,因为需要进行不必要的大小计算。
英文:
You can check the format string as follows:
#include <fmt/core.h>
int main() {
constexpr auto s = fmt::format_string<int>("{:d}");
}
This has no runtime effect as you can see from the generated assembly (https://godbolt.org/z/Eqxa1hf5T):
main:
mov eax, 0
ret
Normally format_string
is used as a function argument so that the check is done automatically when the function is called, e.g.
template <typename... Args>
void log(fmt::format_string<Args...> fmt, Args&&... args) {
// Format string has already been checked at this point.
// Do the logging.
}
FMT_STRING
is a legacy mechanism to do the compile-time checks and I wouldn't recommend using it.
I wouldn't recommend using FMT_COMPILE
for checks either because not all formatters work at compile time and is less efficient because it needs to do unnecessary size computation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论