5 ms·
I like the technique to generate compile error when there is a mismatch between format string and actual argument list and type - This is what only statically t
by innover 12y ago
I like the technique to generate compile error when there is a mismatch between format string and actual argument list and type - This is what only statically typed language can do and dynamically typed languages like python can't do.
Maybe this technique could be applied in printf or other C++ APIs using format string.
- unwind 12y agoNot sure if you're being ironic, but you're describing e.g. GCC's "format" function attribute (https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g_t_0040code_007bformat_007d-function-attribute-2889 https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#...), it makes the compiler verify the arguments for printf() and other format-string functions. It's not new, it has been in GCC for quite a number of years (not sure how to check this quickly). UPDATE: I found a list (https://ohse.de/uwe/articles/gcc-attributes.html#func-format https://ohse.de/uwe/articles/gcc-attributes.html#func-format) that says format was added in GCC 2.3-3.4 (whatever the range means). GCC 3.4.0 was released on April 18 2004. Now I'm sad I didn't say "ten years" above, as my original hunch was. :)
- nly 12y agoSpecific to printf and scanf format codes though, not under the control of the programmer.
- nly 12y agoboost::format does away with format codes altogether... you just use '%' place holders. Using constexpr string literal evaluation would be useful for counting them at compile time though.