Search code examples
c++fmt

{fmt}: Will a named argument be ignored if it doesn't exist in the formatting string?


The following code compiles just fine and produces the string "abc":

fmt::format("abc", fmt::arg("x", 42));

godbolt

So it looks like named arguments that are missing in the format string are just ignored.

My question is: Is that by design or is it a bug?

I'm asking because I'm having a use case for this "feature". So I want to make sure that this is neither UB nor that it will be "fixed" in future.

I already skimmed through the docs but couldn't find this use case.


Solution

  • This is by design. Unused formatting arguments are essentially the same as unused arguments to any other function and are not an error. This is the case in {fmt} and Python's str.format it is modeled after as well as printf.