Fmt: FMT_COMPILE vs FMT_STRING vs ""_format()

Created on 14 Jul 2020  路  2Comments  路  Source: fmtlib/fmt

The documentation is not extremely clear about the difference between the 3 ways I found to have compile-time format string checks. I would like to try to PR an improvement to the documentation, but first I need to be sure

  • Is there a difference as to what they can check? In my quick survey I could not find an example rejected by one but not the others.
  • Is there a preferred one? Compiler verbose output seems shorter with the FMT_STRING version, but not using a macro is nice, or is the speed boost from FMT_COMPILE usually worth it?

Bonus question, is there one which has a chance to land in std::format?

Most helpful comment

Is there a difference as to what they can check?

Yes, FMT_STRING always does compile-time checks while _format only if a compiler supports UDL templates extensions.

Is there a preferred one?

FMT_STRING and FMT_COMPILE serve different purposes. If you only need checks then use FMT_STRING. If you want to avoid format string parsing overhead then use FMT_COMPILE (this is rarely needed) and note that:

Format string compilation can generate more binary code compared to the default API and is only recommended in places where formatting is a performance bottleneck.

All 2 comments

Is there a difference as to what they can check?

Yes, FMT_STRING always does compile-time checks while _format only if a compiler supports UDL templates extensions.

Is there a preferred one?

FMT_STRING and FMT_COMPILE serve different purposes. If you only need checks then use FMT_STRING. If you want to avoid format string parsing overhead then use FMT_COMPILE (this is rarely needed) and note that:

Format string compilation can generate more binary code compared to the default API and is only recommended in places where formatting is a performance bottleneck.

Thanks for clarifying. I would say the documentation could be more explicit. I gave it a try.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

barcharcraz picture barcharcraz  路  4Comments

alex3d picture alex3d  路  6Comments

ugenehan picture ugenehan  路  3Comments

jwtowner picture jwtowner  路  6Comments

beojan picture beojan  路  5Comments