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

Duplicated named arguments are ignored, but not rejected #4282

Open
lewkok opened this issue Jan 1, 2025 · 4 comments
Open

Duplicated named arguments are ignored, but not rejected #4282

lewkok opened this issue Jan 1, 2025 · 4 comments

Comments

@lewkok
Copy link

lewkok commented Jan 1, 2025

Hi,
Is example like:

#include <fmt/format.h>

int main() {
  using namespace fmt::literals;
  fmt::print("{abc} {1}", "abc"_a = 42, "abc"_a = 43 );
}

This prints: "42 43"

Or this example:

#include <fmt/format.h>

int main() {
  using namespace fmt::literals;
  fmt::print("{abc}", "abc"_a = 42, "abc"_a = 43 );
}

This prints: "42"

Is this semantics like this by design or did it happen by mistake?

@lewkok lewkok changed the title Duplicated named arguments are ignored for Duplicated named arguments are ignored Jan 1, 2025
@lewkok lewkok changed the title Duplicated named arguments are ignored Duplicated named arguments are ignored, but not rejected Jan 1, 2025
@vitaut
Copy link
Contributor

vitaut commented Jan 1, 2025

That's just an implementation artifact and I think we could add validation.

@dinomight
Copy link
Contributor

Do you know what behavior you're expecting here? I started playing around with this idea a little bit and can get it to throw an exception at runtime if two named args have the same name. It doesn't seem like it can be caught at compile time without using static named args. Is that the expectation?

@vitaut
Copy link
Contributor

vitaut commented Feb 22, 2025

Do you know what behavior you're expecting here?

Duplicate should give an exception.

@dinomight
Copy link
Contributor

dinomight commented Feb 25, 2025

At the moment, I can verify the following:

fmt::print("{bar}", "bar"_a=42, "bar"_a=43);  // Fails to compile due to duplicate args.
fmt::print("{bar}", fmt::args("bar", 42), fmt::args("bar", 43));  // Throws exception at runtime due to duplicate args.

What might be helpful is some direction on the static compiled strings because the following still shows the existing behavior with my changes:

fmt::print(FMT_COMPILE("{bar}"), "bar"_a=42, "bar"_a=43);  // Compiles and outputs `42`.

I'm assuming if we can stop compile for the non-type template parameters, it should also work for compiled format strings?

Edit:
There's another issue tracking FMT_COMPILE: #4124 I would assume as that is resolved, perhaps it could handle this case as well.

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

No branches or pull requests

3 participants