Skip to content

Bug in basic_json::operator[] const overload #135

Closed
@dariomt

Description

The following code

// note the *const*
const json object =
{
        {"one", 1}, {"two", 2}, {"three", 2.9}
};
std::cout << object.size() << '\n';
std::cout << object["doh!"] << '\n'; // element not found
std::cout << object.size() << '\n'; // const object modified!

prints

3
null
4

Allowing a const object to change like this is not a good idea.
I think this is a bug: either the const overload of operator[] throws if the key cannot be found, or there is no such const overload (e.g. std::map does not have it).

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions