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 error when mixing named argument with automatic indexing #3105

Closed
rbrich opened this issue Sep 18, 2022 · 5 comments · Fixed by #3214
Closed

Compile-time error when mixing named argument with automatic indexing #3105

rbrich opened this issue Sep 18, 2022 · 5 comments · Fixed by #3214

Comments

@rbrich
Copy link
Contributor

rbrich commented Sep 18, 2022

Minimal reproducer (godbolt):

fmt::print("{} {a}", 1, "a"_a = 2);

I believe this is supposed to compile, but it causes:

on_error("cannot switch from automatic to manual argument indexing");

Are the named arguments considered as "indexed", or is it a bug?

It works with fmt::runtime, so if nothing else, there is a problem with consistency between compile-time and runtime checking.

@vitaut
Copy link
Contributor

vitaut commented Sep 19, 2022

Good catch, thanks. We should make the diagnostic consistent but in the meantime you can use manual indexing as a workaround:

fmt::print("{0} {a}", 1, "a"_a = 2);

@rbrich
Copy link
Contributor Author

rbrich commented Sep 21, 2022

Thanks, I know it works with indexed parameters.

Let me add some more details about the use-case: I encountered the issue when I tried to enable compile-time checks for logging in our company, where we already have many messages in code. Changing them to indexed would not practical, nor very nice to the user of such API. The named argument is used to log "strerror", similarly as was common with printf, i.e. original %m is replaced by fmt {m}. This is achieved by adding a named argument "m"_a as the last argument after what user provides in a log function. I also have a similar implementation in open-source here.

It would be nice if such a logging function could have the format checked in compile-time, but I understand that the named argument support is still somewhat experimental.

rbrich added a commit to rbrich/fmt that referenced this issue Nov 30, 2022
@rbrich
Copy link
Contributor Author

rbrich commented Nov 30, 2022

@vitaut What do you think about the fix I posted in #3214 ?

@rbrich
Copy link
Contributor Author

rbrich commented Nov 30, 2022

Btw. I don't really understand why it should not work with indexed args.

This doesn't seem any problematic to me:

fmt::format("{} {2}",  1, 2)

Switching back to automatic ({1} {}) should not be allowed, because it's not clear which index should be selected next. But the other way is quite clear (just use the provided index instead of the next automatic one).

@rbrich
Copy link
Contributor Author

rbrich commented Nov 30, 2022

But I see it's the same in Python, so it probably has some reason:

>>> "{} {}".format(1, 2)
'1 2'
>>> "{} {2}".format(1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: cannot switch from automatic field numbering to manual field specification
>>> "{} {a}".format(1, a=2)
'1 2'

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

Successfully merging a pull request may close this issue.

2 participants