{"action":"created","issue":{"url":"https://api.github.com/repos/nlohmann/json/issues/91","labels_url":"https://api.github.com/repos/nlohmann/json/issues/91/labels{/name}","comments_url":"https://api.github.com/repos/nlohmann/json/issues/91/comments","events_url":"https://api.github.com/repos/nlohmann/json/issues/91/events","html_url":"https://github.com/nlohmann/json/issues/91","id":88447264,"number":91,"title":"access by (const) reference","user":{"login":"ropuls","id":9431950,"avatar_url":"https://avatars.githubusercontent.com/u/9431950?v=3","gravatar_id":"","url":"https://api.github.com/users/ropuls","html_url":"https://github.com/ropuls","followers_url":"https://api.github.com/users/ropuls/followers","following_url":"https://api.github.com/users/ropuls/following{/other_user}","gists_url":"https://api.github.com/users/ropuls/gists{/gist_id}","starred_url":"https://api.github.com/users/ropuls/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ropuls/subscriptions","organizations_url":"https://api.github.com/users/ropuls/orgs","repos_url":"https://api.github.com/users/ropuls/repos","events_url":"https://api.github.com/users/ropuls/events{/privacy}","received_events_url":"https://api.github.com/users/ropuls/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/nlohmann/json/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":{"login":"nlohmann","id":159488,"avatar_url":"https://avatars.githubusercontent.com/u/159488?v=3","gravatar_id":"","url":"https://api.github.com/users/nlohmann","html_url":"https://github.com/nlohmann","followers_url":"https://api.github.com/users/nlohmann/followers","following_url":"https://api.github.com/users/nlohmann/following{/other_user}","gists_url":"https://api.github.com/users/nlohmann/gists{/gist_id}","starred_url":"https://api.github.com/users/nlohmann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nlohmann/subscriptions","organizations_url":"https://api.github.com/users/nlohmann/orgs","repos_url":"https://api.github.com/users/nlohmann/repos","events_url":"https://api.github.com/users/nlohmann/events{/privacy}","received_events_url":"https://api.github.com/users/nlohmann/received_events","type":"User","site_admin":false},"milestone":null,"comments":21,"created_at":"2015-06-15T14:44:10Z","updated_at":"2015-06-29T05:54:24Z","closed_at":null,"body":"Hi Niels,\r\n\r\nyet another question. I try to avoid copying whereever possible (during serialization). So when:\r\n\r\n```c++\r\njson j = \"foo\";\r\nif (j.is_string) {\r\n  const std::string &s = j.get<const std::string &s>();\r\n  // do something with s\r\n}\r\n``` \r\n\r\nthe compiler tells us:\r\n```\r\njson.hpp:1010: error: forming pointer to reference type 'const std::basic_string<char>&'\r\n         return get_impl(static_cast<T*>(nullptr));\r\n                        ^\r\n```\r\n\r\nwith a static cast:\r\n\r\n```c++\r\n  const std::string &s = static_cast<const std::string &>(j);\r\n```\r\n\r\nthe compiler is happy, but apparently returns a reference to a temporary, so s is dangling.\r\n\r\nHave you got any hints on how to access underlying memory (for string, int, ...) so we can avoid copying the values?\r\n\r\nwould \r\n```c++\r\ntemplate<typename T> T* get_ptr();\r\ntemplate<typename T> const T* get_ptr<T>() const;\r\n```\r\nbe acceptable?\r\n\r\nThanks and cheers,\r\n  Roman\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/nlohmann/json/issues/comments/116454098","html_url":"https://github.com/nlohmann/json/issues/91#issuecomment-116454098","issue_url":"https://api.github.com/repos/nlohmann/json/issues/91","id":116454098,"user":{"login":"ropuls","id":9431950,"avatar_url":"https://avatars.githubusercontent.com/u/9431950?v=3","gravatar_id":"","url":"https://api.github.com/users/ropuls","html_url":"https://github.com/ropuls","followers_url":"https://api.github.com/users/ropuls/followers","following_url":"https://api.github.com/users/ropuls/following{/other_user}","gists_url":"https://api.github.com/users/ropuls/gists{/gist_id}","starred_url":"https://api.github.com/users/ropuls/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ropuls/subscriptions","organizations_url":"https://api.github.com/users/ropuls/orgs","repos_url":"https://api.github.com/users/ropuls/repos","events_url":"https://api.github.com/users/ropuls/events{/privacy}","received_events_url":"https://api.github.com/users/ropuls/received_events","type":"User","site_admin":false},"created_at":"2015-06-29T05:54:24Z","updated_at":"2015-06-29T05:54:24Z","body":"Hi Niels,\r\n\r\nthanks a lot for the recent additions!\r\n\r\nRegarding get_ptr at line 1929: IMO, there shall be \r\n\r\n```c++\r\nPointerType get_ptr() noexcept {...}\r\n``` \r\n\r\nplus\r\n\r\n```c++\r\nconst PointerType get_ptr() const noexcept {...}\r\n```\r\n\r\nIf the object itself is *const*, it shall not internally cast to *non-const* (as in line 1939), and shall not expose a non-const pointer (let the user cast the const'ness away, then he's responsible).\r\n\r\n Besides, everything is fine for me, and I appreciate the latest changes. Also, I am happy with all accessor versions, and IMO you cannot protect an API user from himself if he's completely unwilling to read the documentation / header comments about pre- and post-conditions.\r\n\r\nThanks a lot, and case closed for me (except you'd want to have the const/non-const overloads for get_ptr).\r\n\r\nCheers,\r\n  Roman\r\n\r\nPS: Niels: did you get my email about cpp1x.com on gmail?\r\n\r\n"}}