Fix #458: Cannot assign string to function that expects std::string_view#697
Conversation
…strings Register std::string_view as the "string_view" user type with a built-in implicit conversion from std::string, so a ChaiScript string can be passed directly to a C++ function taking std::string_view. Adds the reverse explicit conversion via string(sv) / to_string(sv), plus basic queries (size, length, empty, data) and comparison operators on string_view. String-style methods that take size_t (substr, find, ...) are intentionally not duplicated on string_view: with the implicit conversion in place they would create dispatch ambiguity for calls like string.substr(int, int). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Good catch — the rationale I gave in the PR description doesn't actually hold up under scrutiny. I claimed registering
So for
The There's also a real motivation to add them: I'd suggest a follow-up that:
Want me to open a follow-up PR with that? |
|
@leftibot update this pr with a more robust string_view binding |
Adds substr, find/rfind/find_first_of/find_last_of/find_first_not_of/ find_last_not_of, starts_with/ends_with, and operator[] to string_view_type, mirroring string_type's search/substring surface so script authors can traverse a buffer through string_view without allocating. Sharing method names with string_type creates an ambiguity in dispatch_with_conversions when the script call needs arithmetic conversion (e.g. myString.substr(int, int)): both string::substr and string_view::substr match-except-for-arithmetic, and the existing const/non-const tiebreaker doesn't apply. Extend dispatch_with_conversions to prefer the candidate whose first/receiver parameter type exactly matches the actual receiver, mirroring the deprioritization already in dispatch(). With this in place, myString.substr(1, 2) still resolves to string::substr while mySV.substr(1, 2) resolves to string_view::substr (returning a string_view). Updates the doc comment on string_view_type and adds a compiled test that locks in both dispatch directions plus the new search/substring methods. Requested by @lefticus in PR ChaiScript#697 review. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Addressed review feedback from @lefticus. New commits: |
|
Hi @lefticus — I tried to address the review feedback but couldn't produce a working change. May need human attention. |
Automated fix by @leftibot.
What changed
Files
Closes #458
Triggered by @lefticus.