[英]Deduce format specifier from data type?
Is it possible to deduce the format specifier programmatically for a data type? 是否有可能以编程方式为数据类型推断出格式说明符? For instance if the print is for a long it automatically does something like:
例如,如果打印很长时间,它会自动执行以下操作:
printf("Vlaue of var is <fmt_spec> ", var);
I also feel it would reduce some errors on part of developer since something like 我也觉得它可以减少部分开发人员的错误,因为类似的东西
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
I understand that the latter two do have warnings but wouldn't it be better if errors were thrown since in most cases it is going to be problematic? 我知道后两者确实有警告,但如果错误抛出则不会更好,因为在大多数情况下会出现问题吗? Or am I missing something or are there constructs already in place to do so ?
或者我错过了什么或者是否已经有了这样的构造?
Deduce format specifier from data type?
从数据类型中推断格式说明符?
No . 不 。
As melpomene stated: 正如melpomene所说:
"Format specifiers aren't just for types. Eg %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." “格式说明符不仅仅适用于类型。例如,
%o
和%x
都采用unsigned int
; %e
, %f
, %g
都需要double
; %d
, %i
, %c
都采用int
。这就是为什么你可以' t(一般来说)从论证中推断出来。“
Point is that if such a functionality existed, then would it deduce unsiged int
to %o
or %x
, for example? 重点是,如果存在这样的功能,那么它
unsiged int
推导到%o
或%x
吗? And so on . 等等 。 .
。 .
。
About whether some cases should issue a warning or an issue, you should think about how casting works in c , and when it does make sense to allow something or not. 关于某些案例是否应该发出警告或问题,您应该考虑如何在c中进行投射,以及何时允许或不允许某些事情。 In GCC, you could of course treat warning(s) as error(s):
在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.
Is it possible to deduce the format specifier programmatically for a data type?
是否有可能以编程方式为数据类型推断出格式说明符?
Not easily nor directly with printf()
, yet... 不容易,也不直接与
printf()
,但......
Yes, with limitations to a select set of types, by using of _Generic
. 是的,使用
_Generic
限制选择的一组类型。
This could be done various ways and used with *printf()
with great dificulty, yet I found a similar approach to print data, without specifying individual format specifiers in this example: 这可以通过各种方式完成,并与
*printf()
一起使用,但是我发现了类似的打印数据方法,但没有在此示例中指定单独的格式说明符:
Formatted print without the need to specify type matching specifiers using _Generic 格式化打印,无需使用_Generic指定类型匹配说明符
Note: This code has a coding hole concerning pointer math, that I have since patched - though not posted. 注意:这段代码有一个关于指针数学的编码孔,我已修补了 - 尽管没有发布。
GPrintf("Name is ", GP(name), " is ", GP(dob), GP_eol);
The key was to use _Generic(parameter)
to steer the selection of the code used to convert the type to text by having the macro GP(x)
expand to 2 parts: a string and x
. 关键是使用
_Generic(parameter)
来控制用于将类型转换为文本的代码的选择,方法是将宏GP(x)
扩展为2部分:字符串和x
。 Then GPrintf()
interprets the arguments. 然后
GPrintf()
解释参数。
This is akin to @Michaël Roy 's comment, yet staying in C rather than C++. 这类似于@MichaëlRoy的评论,但仍然使用C而不是C ++。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.