Giter Site home page Giter Site logo

Comments (5)

sdkrystian avatar sdkrystian commented on May 30, 2024 2

@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.

grisumbras avatar grisumbras commented on May 30, 2024 1

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.

Elnee avatar Elnee commented on May 30, 2024 1

@sdkrystian Many thanks for the detailed explanation.

from json.

Elnee avatar Elnee commented on May 30, 2024

@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.

Elnee avatar Elnee commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.