Skip to content

inconsistency with string/string_view lookups #3912

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

Open
Spongman opened this issue Jan 8, 2023 · 2 comments
Open

inconsistency with string/string_view lookups #3912

Spongman opened this issue Jan 8, 2023 · 2 comments
Labels
confirmed kind: bug state: please discuss please discuss the issue or vote for your favorite option

Comments

@Spongman
Copy link

Spongman commented Jan 8, 2023

Description

operator string_view() isn't used, whereas operator string() is.

Reproduction steps

https://godbolt.org/z/MYEYG3Gao

Expected vs. actual results

compiles

Minimal code example

#include <iostream>
#include <string>
#include "nlohmann/json.hpp"

using json = nlohmann::json;

struct Foo {
	operator std::string() const { return "foo"; }
};

struct Bar {
	operator std::string_view() const { return "bar"; }
};

int main() {

	json js {
		{ "foo", "string" },
		{ "bar", "string_view" },
	};

	std::cout << js[std::string{"foo"}] << "\n";
	std::cout << js[Foo{}] << "\n";

	std::cout << js[std::string_view{"bar"}] << "\n";
	std::cout << js[Bar{}] << "\n";
}

Error messages

<source>: In function 'int main()':
<source>:26:24: error: no match for 'operator[]' (operand types are 'json' {aka 'nlohmann::json_abi_v3_11_2::basic_json<>'} and 'Bar')
   26 |         std::cout << js[Bar{}] << "\n";
      |                        ^


### Compiler and operating system

linux gcc 12.2

### Library version

trunk

### Validation

- [x] The bug also occurs if the latest version from the [`develop`](https://github.com/nlohmann/json/tree/develop) branch is used.
- [ ] I can successfully [compile and run the unit tests](https://github.com/nlohmann/json#execute-unit-tests).
@nlohmann
Copy link
Owner

I can confirm an error at:

 error: no viable overloaded operator[] for type 'json' (aka 'basic_json<>')
   25 |         std::cout << js[std::string_view{"bar"}] << "\n";
      |                      ~~^~~~~~~~~~~~~~~~~~~~~~~~

There is currently no way to query object keys via std::string_view.

@nlohmann nlohmann added confirmed state: please discuss please discuss the issue or vote for your favorite option labels Nov 29, 2024
@Spongman
Copy link
Author

that's weird, my godbolt link above shows that line compiling just fine in gcc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed kind: bug state: please discuss please discuss the issue or vote for your favorite option
Projects
None yet
Development

No branches or pull requests

2 participants