Skip to content

Destructor is called when filling object through assignement #1050

@vindex10

Description

@vindex10

Bug Report

  • What is the issue you have?
    Destructor being called in an unexpected place.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

#include <iostream>
#include "nlohmann/json.hpp"

using namespace std;
using json = nlohmann::json;

class State {
public:
    double x;
    ~State() {
        cout << "destroyed" << endl;
    }
};

void from_json(const json& j, State& p) {
    try {
        p.x = j.at("x").get<double>();
    } catch(json::type_error& e) {
        std::cerr << e.what() << std::endl;
    } catch(json::out_of_range& e) {
        std::cerr << e.what() << std::endl;
    }
}

int main() {
    json data;
    State s;
    data["x"] = 10.;
    s = data;
    cout << s.x << endl;
    return 0;
}

Output is:

destroyed
10
destroyed
  • What is the expected behavior?
    do not call destructor during assignment

  • And what is the actual behavior instead?
    destructor is called

  • Which compiler and operating system are you using? Is it a supported compiler?

g++ (Gentoo 6.4.0-r1 p1.3) 6.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  • Did you use a released version of the library or the version from the develop branch?
    checked for both: master and develop branch.

Could you please describe, whether it is a feature or a bug :) By default assignment operator doesn't call destructor, that is why the behavior seems to me unexpected.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: invalidthe issue is not related to the librarysolution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions