-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
accessing key by reference #1098
Comments
I'll check. |
This is the function: typename object_t::key_type key() const
{
assert(m_object != nullptr);
if (JSON_LIKELY(m_object->is_object()))
{
return m_it.object_iterator->first;
}
JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators"));
}
|
Yes, I saw this function. Yes, you can return a reference to std::map key here. I bet it returns by value because there is another key() implementation for array that returns index id converted to string, but (unless there something prevents it) you should be able to have one |
@crusader-mike What do you think of 90eb0a9? |
I think proper solution is to have But it is probably too late as this is breaking change API-wise... Now going back to least invasive approach you were considering -- it looks ok with few notes:
|
on second thought you don't really need |
You are right: it makes more sense to only calculate the array key when it is actually accessed. I implemented that. Regarding your other remarks:
|
There are few ways, for example:
or you could do it via in template class:
or in C++17 you could use inline variables:
or you could convince your MSVC linker to discard all but first
No, you misunderstood me -- idea was to remove array-related logic from
Well, it isn't really complex -- just gotta know about the bug and be careful in owner type constructor. |
... followup thought -- instead of trying to "fix" |
Well, I don't want to add a breaking API change for this. I shall merge the feature branch and would be happy about pull requests. |
adding |
Is there any way to access
key
in object by reference? Currently(const_)iterator::key()
returns string by value causing extra copy every time I need to access it.Is there any reason why that string gets returned by value?
The text was updated successfully, but these errors were encountered: