Comments (5)
@Elnee Per [class.temporary] p4, temporary objects are destroyed after evaluating the full-expression in which they appear (which in this case is the initialization of object
). When a temporary object is bound to a reference, its lifetime will be extended to that of the reference if, and only if, one of the conditions specified by [class.temporary] p6 are met. Binding a reference to the result of calling a non-static member function of a temporary object does not satisfy any of these conditions, so the lifetime is not extended.
from json.
json::parse
returns a json::value
, which you don't store anywhere. You instead get a reference to its subobject of type json::object
. At the end of that statement the value
goes out of scope and destroys its internal object
. After that you use a dangling reference. In order to fix this you should store either the value
or the object
:
const auto value = boost::json::parse(json);
const auto& object = value.as_object();
// or
const auto object = boost::json::parse(json).as_object();
from json.
@sdkrystian Many thanks for the detailed explanation.
from json.
@grisumbras Thank you, it works. But I don't understand why using a const-ref
doesn't expand the lifetime of that object.
As I see boost::json::value
has a union returned by as_object()
. Is it because its lifetime is bound to the instance of boost::json::value
?
from json.
And also, isn't it a bit misleading that I should think about as_object()
method in such a way? I thought about it as getting the same object but in a different representation (due to the as
prefix). It turned out to return some internals to me.
Doesn't it force me to know some internal library implementation details aside from the interface as a user?
from json.
Related Issues (20)
- remove type aliases HOT 1
- had to download half the internet to read the docs HOT 2
- Confusion regarding result_for HOT 5
- Exception of segment fail when run value_from for tree structuor HOT 2
- boost::json::value_to on std::bitset? HOT 6
- Regression: serializing vector<> of objects HOT 4
- Linux gcc compilation failures with --pedantic-errors flag: error: extra ‘;’ HOT 1
- Value Iteration HOT 5
- Question: how to determine the required size for temp buffers? HOT 7
- If an unsigned integer is serialized, deserializing back to the unsigned integer fails HOT 8
- Comment Only: Logo HOT 3
- Stack exaustion while value_from(std::filesystem::path) HOT 5
- Small help for non eagle eyed people HOT 2
- Documentation inconsistency about exceptions HOT 2
- Add a convenience member function to call value_to HOT 5
- boost::json::value::is_uint64() behaves unintuitively HOT 6
- Add support to universal tag_invoke for class as struct with macro BOOST_DESCRIBE_CLASS HOT 10
- Unable to extract boolean value in numberic format HOT 1
- Add a shorthand to masquerade a type parsing/serializing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json.