Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile time checking of format string? #295

Closed
paulsapps opened this issue Mar 17, 2016 · 5 comments
Closed

Compile time checking of format string? #295

paulsapps opened this issue Mar 17, 2016 · 5 comments

Comments

@paulsapps
Copy link

@paulsapps paulsapps commented Mar 17, 2016

Would it be possible to add this feature using constexpr compile time format string checking? For example:

fmt::format("{} {}", "blah");

Would result in compile time instead of run time error..

@nabijaczleweli
Copy link

@nabijaczleweli nabijaczleweli commented Mar 17, 2016

No, TTBOMK, because one can't pass string literals as template arguments, so compile-time string inspection is well-nigh impossible.

@vitaut
Copy link
Contributor

@vitaut vitaut commented Mar 17, 2016

Unfortunately it is not possible (at least with a usable API) at the moment because of the difficulties of handling string literals that @nabijaczleweli mentioned.

However, you can get compile-time checking of printf-style format strings (but not Python-like format strings) as described in #62 (comment).

@vitaut vitaut closed this Mar 17, 2016
@vitaut
Copy link
Contributor

@vitaut vitaut commented Nov 6, 2017

Compile-time format string checks are now available: http://zverovich.net/2017/11/05/compile-time-format-strings.html

@mattgodbolt
Copy link

@mattgodbolt mattgodbolt commented Sep 24, 2020

For those like me coming here from Google and finding that linked site is down, the main README has this example:

std::string s = fmt::format(FMT_STRING("{:d}"), "don't panic");
@vitaut
Copy link
Contributor

@vitaut vitaut commented Sep 24, 2020

There are also docs for the most adventurous people: https://fmt.dev/latest/api.html#compile-api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants