Opened 11 years ago

Closed 5 years ago

#6815 closed Feature Requests (wontfix)

Compile time checking of format string (e.g. in printf)

Reported by: d.starosud@… Owned by: James E. King, III
Milestone: To Be Determined Component: format
Version: Boost Development Trunk Severity: Not Applicable
Keywords: Cc: d.starosud@…

Description

It would be great to have possibility to check correspondence and correctness of format and arguments of printf function in compile time. This functionality would fill in the lack of expressive power of user-defined literals form C++11, I mean inability to use template-user-defined literal for character strings (e.g. "qwerty 123"_format). In attachment you can find my implementation of it. It would be really great if it or maybe better improved variant of it will be in boost.

Attachments (4)

templ_literal.h (1.5 KB ) - added by d.starosud@… 11 years ago.
main.cpp (1.8 KB ) - added by d 11 years ago.
main.2.cpp (1.8 KB ) - added by d. 11 years ago.
printf.cpp (7.2 KB ) - added by anonymous 8 years ago.
additional format type checking

Download all attachments as: .zip

Change History (8)

by d.starosud@…, 11 years ago

Attachment: templ_literal.h added

by d, 11 years ago

Attachment: main.cpp added

by d., 11 years ago

Attachment: main.2.cpp added

comment:1 by d.starosud@…, 11 years ago

Sorry, I added two similar files.

comment:2 by anonymous, 9 years ago

Awesome! I've been looking for something like this for years

by anonymous, 8 years ago

Attachment: printf.cpp added

additional format type checking

comment:3 by James E. King, III, 5 years ago

Owner: changed from Samuel Krempp to James E. King, III
Status: newassigned

comment:4 by James E. King, III, 5 years ago

Resolution: wontfix
Status: assignedclosed

This implementation is incomplete so I am going to mark it as wontfix. Seems like a cool idea but it wouldn't support dynamically generated format specifications and doesn't support the complete syntax of boost format... unit tests should be used in consuming code to ensure no exceptions occur.

An interesting place to make a change like this could be in the gcc/clang static analyzers... if you build with "-Wformat" they can stop a build on a static format string that is malformed or when the wrong sized argument is given for a conversion specifier. Perhaps adding support for boost::format to that would be useful, but it still wouldn't take care of dynamically generated format specifications.

Note: See TracTickets for help on using tickets.