从数据类型推导出格式说明符?
Deduce format specifier from data type?
是否可以通过编程方式为数据类型推断格式说明符?例如,如果打印时间很长,它会自动执行如下操作:
printf("Vlaue of var is <fmt_spec> ", var);
我也觉得它会减少部分开发人员的一些错误,因为
printf("Name is %s",int_val); //Oops, int_val would be treated as an address
printf("Name is %s, DOB is",name,dob); // missed %d for dob
printf("Name is %s DOB is %d", name);//Missed printing DOB
我知道后两者确实有警告,但如果抛出错误不是更好吗,因为在大多数情况下它会出现问题?还是我遗漏了一些东西,或者是否已经有结构可以这样做?
Deduce format specifier from data type?
没有.
正如美尔波美所说:
"Format specifiers aren't just for types. E.g. %o
and %x
both take unsigned int
; %e
, %f
, %g
all take double
; %d
, %i
, %c
all take int
. That's why you can't (in general) deduce them from the arguments."
要点是,如果存在这样的功能,那么它将 unsiged int
推断为 %o
或 %x
,例如?等等 。 . .
关于某些情况是否应该发出警告或问题,您应该考虑在 c 中转换是如何工作的,以及什么时候允许或不允许。在 GCC 中,您当然可以将警告视为错误:
-Werror
Make all warnings into errors.
-Werror=
Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings; for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect.
The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)
Note that specifying -Werror=foo automatically implies -Wfoo. However, -Wno-error=foo does not imply anything.
如你所见here.
Is it possible to deduce the format specifier programmatically for a data type?
使用 printf()
既不容易也不直接,但是...
是的,使用 _Generic
限制了 select 组类型。
这可以通过多种方式完成,并且很难与 *printf()
一起使用,但我发现了一种类似的打印数据的方法,在此示例中没有指定单独的格式说明符:
Formatted print without the need to specify type matching specifiers using _Generic
注意:这段代码有一个关于指针数学的编码漏洞,我已经修补了——虽然没有发布。
GPrintf("Name is ", GP(name), " is ", GP(dob), GP_eol);
关键是使用 _Generic(parameter)
通过将宏 GP(x)
扩展为 2 个部分来控制用于将类型转换为文本的代码的 selection:一个字符串和 x
。然后 GPrintf()
解释参数。
这类似于 的评论,但仍使用 C 而不是 C++。
是否可以通过编程方式为数据类型推断格式说明符?例如,如果打印时间很长,它会自动执行如下操作:
printf("Vlaue of var is <fmt_spec> ", var);
我也觉得它会减少部分开发人员的一些错误,因为
printf("Name is %s",int_val); //Oops, int_val would be treated as an address
printf("Name is %s, DOB is",name,dob); // missed %d for dob
printf("Name is %s DOB is %d", name);//Missed printing DOB
我知道后两者确实有警告,但如果抛出错误不是更好吗,因为在大多数情况下它会出现问题?还是我遗漏了一些东西,或者是否已经有结构可以这样做?
Deduce format specifier from data type?
没有.
正如美尔波美所说:
"Format specifiers aren't just for types. E.g. %o
and %x
both take unsigned int
; %e
, %f
, %g
all take double
; %d
, %i
, %c
all take int
. That's why you can't (in general) deduce them from the arguments."
要点是,如果存在这样的功能,那么它将 unsiged int
推断为 %o
或 %x
,例如?等等 。 . .
关于某些情况是否应该发出警告或问题,您应该考虑在 c 中转换是如何工作的,以及什么时候允许或不允许。在 GCC 中,您当然可以将警告视为错误:
-Werror
Make all warnings into errors.
-Werror=
Make the specified warning into an error. The specifier for a warning is appended; for example -Werror=switch turns the warnings controlled by -Wswitch into errors. This switch takes a negative form, to be used to negate -Werror for specific warnings; for example -Wno-error=switch makes -Wswitch warnings not be errors, even when -Werror is in effect.
The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)
Note that specifying -Werror=foo automatically implies -Wfoo. However, -Wno-error=foo does not imply anything.
如你所见here.
Is it possible to deduce the format specifier programmatically for a data type?
使用 printf()
既不容易也不直接,但是...
是的,使用 _Generic
限制了 select 组类型。
这可以通过多种方式完成,并且很难与 *printf()
一起使用,但我发现了一种类似的打印数据的方法,在此示例中没有指定单独的格式说明符:
Formatted print without the need to specify type matching specifiers using _Generic
注意:这段代码有一个关于指针数学的编码漏洞,我已经修补了——虽然没有发布。
GPrintf("Name is ", GP(name), " is ", GP(dob), GP_eol);
关键是使用 _Generic(parameter)
通过将宏 GP(x)
扩展为 2 个部分来控制用于将类型转换为文本的代码的 selection:一个字符串和 x
。然后 GPrintf()
解释参数。
这类似于