Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



Not 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#...), 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) 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. :)


Specific to printf and scanf format codes though, not under the control of the programmer.


boost::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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: