Skip to content

operator[] overloads for const char* keys - #26

Merged
jeandet merged 1 commit into
LaboratoryOfPlasmaPhysics:masterfrom
PhilipDeegan:char-key-overload
Nov 19, 2026
Merged

jeandet merged 1 commit into
LaboratoryOfPlasmaPhysics:masterfrom
PhilipDeegan:char-key-overload

Conversation

@PhilipDeegan

@PhilipDeegan PhilipDeegan commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

With the implicit operator T&() conversion, a Dict with a long leaf (std::int64_t / std::ptrdiff_t on LP64) makes dict["key"] ambiguous on clang:

  • member operator[](std::string const&): exact on dict, user-defined conversion on "key"
  • built-in "key"[dict] i.e. operator[](ptrdiff_t, char const*): user-defined conversion on dict (via operator long&()), exact on "key"

GCC accepts it (it skips conversion templates when forming built-in candidates), clang rejects it. Seen in cppconfig CI with a Dict<std::int64_t, ...>.

Adding operator[](const char*) (and const) gives string literals an exact match so the member always wins, while keeping the implicit conversion (T x = dict[...]).

Adds a regression test, which fails to compile on clang without the fix.

With the implicit operator T&() conversion, a Dict with a long leaf
(std::int64_t / std::ptrdiff_t on LP64) makes dict["key"] ambiguous on
clang between the member operator[](std::string const&) and the built-in
"key"[dict]. An exact match overload for const char* resolves it.
@jeandet
jeandet merged commit 46e6deb into LaboratoryOfPlasmaPhysics:master Nov 19, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants