You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I see there were some discussions about adding get_ref(), but I can't see it in the code.
What happened to get_ref()?
I believe that having get_ref() is convenient and safe. It could merely call get_ptr() and throw if it returns nullptr. It makes it easier to traverse a json object when I'm pretty sure about the types it holds. Otherwise I have to constantly check against nullptr myself.
I'd like the following code to work seamlessly:
voidfunc(const std::string&);
json value = "foobar";
func( value.get_ref<const std::string>() );
Currently I'd have to write the following:
voidfunc(const std::string&);
json value = "foobar";
auto p = value.get_ptr<const std::string*>();
if (!p) throw some_exception;
func( *p );
I'll try to provide a PR including tests and docs.
One question: what would be the preferred form? get_ref<const std::string>()
or get_ref<const std::string &>()
To me get_ref is enough to convey that we get a reference, the & in the template parameter is redundant (I feel the same way about the * required by get_ptr by the way).
I have this ready in my branch get_ref.
But I have created that branch off my master where I have all my other PRs already merged.
Do you know how I can produce a PR with only the actual changes done in my branch?
Activity
nlohmann commentedon Oct 6, 2015
After a getter for a value and a pointer I did not follow to implement
get_ref. But I would be glad to have a PR for that.dariomt commentedon Oct 7, 2015
I'll try to provide a PR including tests and docs.
One question: what would be the preferred form?
get_ref<const std::string>()or
get_ref<const std::string &>()To me get_ref is enough to convey that we get a reference, the
&in the template parameter is redundant (I feel the same way about the*required by get_ptr by the way).dariomt commentedon Oct 16, 2015
I have this ready in my branch get_ref.
But I have created that branch off my master where I have all my other PRs already merged.
Do you know how I can produce a PR with only the actual changes done in my branch?
nlohmann commentedon Dec 7, 2015
Hi @dariomt, sorry for not answering earlier.
As for your first question: I would prefer
get_ref<const std::string &>- it makes the getting of a reference explicit.For your second question, I unfortunately have no answer.
nlohmann commentedon Dec 16, 2015
Any news here?
dariomt commentedon Dec 16, 2015
My changes already implemented
get_ref<const std::string &>, so that's good.However, I'm still trying to get the branch in proper shape to make the PR easy for you, git newbie here :)
nlohmann commentedon Dec 27, 2015
Ok - looking forward to it!
nlohmann commentedon Jan 14, 2016
@dariomt - any news on this?
dariomt commentedon Jan 18, 2016
Sorry, I haven't had time to look into this.
I should have time this week.
dariomt commentedon Jan 18, 2016
PR ready!
I think I've merged everything correctly.
nlohmann commentedon Jan 20, 2016
Thanks @dariomt for the good job!