-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Static method invites inadvertent logic error. #1433
Comments
Some thoughts on this:
I did not fully grasp the idea with the allocator. Unfortunately, allocator support is very weak (see #161 for an early issue). Maybe this could be a base for an additional function. However, using the SAX parser interface would also allow to create a JSON value without having the parser allocate it. See the documentation for more information. |
I've seen linter checks for calling a static function on an object. I think we can safely put |
Thanks for the response. I would imagine it's possible to deduce whether [[nodiscard]] (or compiler-specific attributes such as It would be nice to have a DOM built as it is now, but in a memory buffer defined by a custom allocator. This could improve performance for 'JSON views' dramatically. But I think this is a subject for another PR. I'll have a think about it. |
quick example: https://godbolt.org/z/-tLO1K |
@gregmarr @madmongo1 Thanks for the hint wrt. nodiscard! This would be definitely a good addition - also for some other functions maybe. @madmongo1 I'm looking forward to your ideas on how to improve the memory management! |
The right way to do this would probably be to use
This way, modern compilers compiling code with C++11 flags are still allowed to apply the attribute. |
I am thinking about it. It's not trivial because we'd want to propagate the rebound allocator to the vector, map and string templates. |
Maybe I'm missing something (and this is definitely out of the scope of this issue), but can't you get where you want memory-wise pretty easily already?
|
@FrancoisChabot Please see an old discussion in #161 - I fear it is not so easy. |
Fixed with e89c946 |
Awesome. Thank you.
—
… You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1433 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AE7syeYvDWgCI8mTYFfDG-zdQt5Fxe9dks5vFIB4gaJpZM4aAldz>
.
|
I recenly wrote code like this:
It compiled fine, but subsequent tests failed (i.e. the expected result was not realised at runtime).
2 hours later I realised my mistake.
json::parse
is a static method that returns a json object. But the compiler was not able to help me discover my mistake early.Suggestions:
nodiscard
add a
[[nodiscard]]
directive to the function when detected that the compiler supports it.deprecate confusing interface
possibly deprecate
static json json::parse(...)
deduce json type through ADL
possibly provide an ADL free function overload suite such that the user may call:
If users wish to have a parse that returns a newly constructed json object, this can be self provided, example:
More complex user-defined functions may want to deduce the allocator from the source, making it easier to use the library with custom allocators such as arenas, this improving performance.
E.g.
see above
I intuitively expected
parse
to be a member function which would mutatejbody
.The return value was silently discarded.
clang, gcc8, fedora29, osx.
develop
branch?released
N/A
The text was updated successfully, but these errors were encountered: