Modifying a value in a boost::json::object - c++

I've been trying to use a boost::json::object to store a JSON object with several values and then modify values as needed, but have been unable to figure out a way to perform the modifications, even after reviewing information on C++ references and overloaded methods differing only by return type which seemed to be pertinent.
In the following test code, I create a brief JSON object in a boost::json::object as illustrated in the "Quick Look" section of the Boost.JSON documentation, read out and display a value from it, attempt to modify that value, and then read out and display the new value.
#include <iostream>
#include <boost/json/src.hpp>
#include <boost/json/object.hpp>
using std::cout;
using std::endl;
int main(int argc, char* argv[]) {
boost::json::object testobj;
testobj["properties"] = { { "timestamp", "Fri Jan 6 02:17:29 PM EST 2023"}, { "textDescription", "This is a Description"} };
const boost::json::string& s = testobj.at("properties").at("textDescription").as_string();
cout << s << endl;
testobj.at("properties").at("textDescription").as_string() = "Hello, World!"; // ERROR
boost::json::string t = testobj.at("properties").at("textDescription").as_string();
cout << t << endl;
return 0;
}
At the noted ERROR line, the following error is generated.
error: passing ‘const boost::json::string’ as ‘this’ argument discards qualifiers [-fpermissive]
testobj.at("properties").at("textDescription").as_string() = "Hello, World!";
^~~~~~~~~~~~~~~
According to the documentation for value::as_string, there are two overloaded as_string() methods, one returning string& and the other returning string const&. Presumably, a returned string& would allow modification of the value, but I can't figure out how to access that particular overload of the method. My understanding was that if the instantiated class was const, then the const overload would be used, and otherwise the non-const overload would be used, but my instantiation is non-const and yet it's not working. Can anyone point out what I'm doing wrong here or provide some guidance on some other way to modify a value like this in a boost::json::object?
Note that I also tried getting a non-const boost::json::string& s reference and assigning a new string to it, but the compiler complained about that as well. Otherwise, everything else in the code above works as expected if the three lines preceding the return 0 line are commented out, so it's just the ERROR line that's problematic.

Non-const value::at overloads were added in version Boost 1.80.0: https://github.com/boostorg/json/commit/95a6297480ec403ccc2cf17ff02476dc7614f60c
It fixes their issue #703:
value::at() returns value with const qualifier even if the value is
non-const data.
Example
value jv1 = {{"key", 1}};
auto& a1 = jv1.as_object().at("key"); // returns value&
auto& a2 = jv1.at("key"); // returns const value&
value& a3 = jv1.at("key"); // compile error
value jv2 = {1, 2, 3};
auto& b1 = jv2.as_array().at(0); // returns value&
auto& b2 = jv2.at(0); // returns const value&
value& b3 = jv2.at(0); // compile error
Note: value.at_pointer() has no such issue. It returns const value&
if the data is const-qualified and returns value& otherwise.
Workaround for Boost 1.79.0:
This works in Boost 1.79.0:
if (auto p = testobj.if_contains("properties"))
if (auto td = p->as_object().if_contains("textDescription"))
td->as_string() = "Hello, World!";
Or even just
testobj["properties"].as_object()["textDescription"] = "Even better?";
See it Live On Boost 1.76.0

Related

c++ "error:passing 'const std::vector<int>' as 'this' argument discards qualifiers [-fpermissive]"

I am trying to implement this POMDP solver with given examples for my decision making problem and I followed the documentation in the repository to build different relevant classes and functions in the header file
class SimpleState: public State {
public:
int position;
int context;
int time;
SimpleState();
SimpleState(int _position, int _context, int _time) :
rat_position(_position),
context(_context),
time(_time) {
}
SimpleState(int _state_id);
~SimpleState();
std::string text() const;
};
SimpleState::SimpleState() {
}
class StarMazeProblem : public DSPOMDP,
public MDP {
protected:
std::vector<std::vector<std::vector<State> > > transition_probabilities_; //state, action, [state, weight]
mutable MemoryPool<SimpleState> memory_pool_;
std::vector<SimpleState*> states_;
mutable std::vector<ValuedAction> mdp_policy_;
public:
enum {CENTER = 0, RIGHT = 1, LEFT = 2};
public:
StarMazeProblem();
int NumStates() const;
void ComputeDefaultActions(std::string type) const;
ParticleUpperBound* CreateParticleUpperBound(std::string name = "DEFAULT") const;//?
ScenarioUpperBound* CreateScenarioUpperBound(std::string name = "DEFAULT",
std::string particle_bound_name = "DEFAULT") const;
ScenarioLowerBound* CreateScenarioLowerBound(std::string name = "DEFAULT",
std::string particle_bound_name = "DEFAULT") const;
}
and in the starmaze.cpp file the relevant lines are
int StarMazeProblem::NumStates() const {
return CONTEXT * POSITIONS * TIME;
}
void StarMazeProblem::ComputeDefaultActions(string type) const {
cerr << "Default action = " << type << endl;
if (type == "MDP") {
const_cast<StarMazeProblem*>(this)->ComputeOptimalPolicyUsingVI();
int num_states = NumStates();
default_action_.resize(num_states);
double value = 0;
for (int s = 0; s < num_states; s++) {
default_action_[s] = policy_[s].action;
value += policy_[s].value;
}
} else {
cerr << "Unsupported default action type " << type << endl;
exit(0);
}
}
ScenarioLowerBound* StarMazeProblem::CreateScenarioLowerBound(string name,
string particle_bound_name="DEFAULT") const {
const DSPOMDP* model = this;
const StateIndexer* indexer = this;
const StatePolicy* policy = this;
ScenarioLowerBound* bound = NULL;
if (name == "TRIVIAL" ) {
bound = new TrivialParticleLowerBound(this);
} else if (name == "RANDOM") {
bound = new RandomPolicy(this,
CreateParticleLowerBound(particle_bound_name));
} else if (name == "MODE" || name == "DEFAULT") {
ComputeDefaultActions("MDP");
bound = new ModeStatePolicy(model, *indexer, *policy,
CreateParticleLowerBound(particle_bound_name));
} else {
cerr << "Unsupported scenario lower bound: " << name << endl;
exit(1);
}
return bound;
}
here I got the following error for the above code:
src/starmaze.cpp:301:36: error: passing 'const std::vector<int>' as 'this' argument discards qualifiers [-fpermissive]
default_action_.resize(num_states);
^
In file included from /opt/local/include/gcc7/c++/vector:64:0,
from ../../../include/despot/interface/lower_bound.h:4,
from ../../../include/despot/core/builtin_lower_bounds.h:4,
from src/starmaze.h:3,
from src/starmaze.cpp:1:
/opt/local/include/gcc7/c++/bits/stl_vector.h:689:7: note: in call to 'void std::vector<_Tp, _Alloc>::resize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]'
resize(size_type __new_size)
^~~~~~
I have basic c++ knowledge and I could not figure out the reason for the error since I followed the examples. Any suggestion?
Important to know before reading
I've looked over this code several times, and cannot find where default_action_ is declared as a const std::vector<int>. This leads me to assume (based on the fact that the compiler recognizes it as such) that the variable is declared in one of the parent classes: public DSPOMDP, public MDP.
You are trying to modify a const vector<int>
This issue is pretty straight-forward.
You cannot change a const std::vector<Ty>
The whole point of making something const is to prevent the programmer from changing it.
Whatever you are trying to do here, you should consider copying the vector data into a new vector, and make the changes to it in the copy.
Okay so let's say you decided to do the naughty deed and remove const from the *.h file wherever it may be. You'll still have an issue (see below)
You are trying to modify the class instance from a const method
This is if you removed const from the vector
The default_action_.resize(num_states); is going to cause the compiler to yell at you.
The issue here is now your method StarMazeProblem::ComputeDefaultActions() is specified as const. This means you cannot change your class instance in any way from within that function/class method. What you are doing is modifying the default_action_ vector by calling resize() on it. This inevitably changes the class instance which is forbidden because your method is declared const and your default_action_ vector is declared somewhere in your class hierarchy. Additionally you are changing the vector directly by assigning it to an rvalue (See code below)
int num_states = NumStates();
default_action_.resize(num_states); // <---- default_action_ cannot be modified
for (int s = 0; s < num_states; s++) {
default_action_[s] = policy_[s].action; // <---- default_action_ cannot be modified
value += policy_[s].value;
}
Solution
You need a copy of the vector you are trying to modify. Going into h files and changing them without knowing 100% exactly what you are doing is never the answer.
Making a temporary vector in the class method, copying the data to it, and manipulating the copy is completely allowed and will not give you any compiler errors; however, this approach may completely avoid your official goal. It looks like you are really trying to change the default_action_ vector and that almost seems like your true goal here.
It's hard for me to give you a definitive answer on this specific topic. Mainly because I do not know what this entire class hierarchy does or what it is for exactly (I briefly looked over the code in the link provided.)
What you'll need to do ultimately is determine why you are trying to change that vector, and why it was made const by the original developers. Then you can determine if there is a better way other than explicitly removing the constness of the vector.
If you are still trying to make changes to the class instance read below
This solution part has to do with the qualifier of the class you are dealing with.
You can either remove the constant qualifier from the class method if it is absolutely necessary to make changes to the class instance from within that method.
OR
Find an alternative method for what you are trying to do, and break it up into multiple method calls on the class instance.
You've made ComputeDefaultActions() const-qualified, but there is very little that says that it should be.
It calls ComputeOptimalPolicyUsingVI() which is not const-qualified since it changes the internal state of *this. You've worked around that by casting away const on this - but that function resizes policy_ to also be NumStates() big which ComputeDefaultActions() depends on - otherwise the internal loop would be very dangerous.
It changes default_action_ by resizeing it and by assigning its elements. This may be a mistake, if the computation made is actually meant to not change *this but ... (see next bullet)
It doesn't return anything. Everything the function does is to change the internal state of *this. The computed value is just dropped at the end of the calculation and it's not even used internally in the function. Either make the function return value or remove value completely.
Suggested changes:
#include <algorithm>
#include <stdexcept>
void StarMazeProblem::ComputeDefaultActions(string type) { // non-const
cerr << "Default action = " << type << endl;
if (type == "MDP") {
ComputeOptimalPolicyUsingVI(); // no casting needed
default_action_.resize(policy_.size()); // dependency made clear
// Use the standard transform function:
std::transform(policy_.begin(), policy_.end(), default_action_.begin(),
[](const auto& p) { return p.action; });
} else {
// friendlier than exit():
throw std::runtime_error("Unsupported default action type " + type);
}
}

Why does std::set seem to force the use of a const_iterator?

Consider the simple program below, which attempts to iterate through the values of a set using NON-const references to the elements in it:
#include <set>
#include <iostream>
class Int
{
public:
Int(int value) : value_(value) {}
int value() const { return value_; }
bool operator<(const Int& other) const { return value_ < other.value(); }
private:
int value_;
};
int
main(int argc, char** argv) {
std::set<Int> ints;
ints.insert(10);
for (Int& i : ints) {
std::cout << i.value() << std::endl;
}
return 0;
}
When compiling this, I get an error from gcc:
test.c: In function ‘int main(int, char**)’:
test.c:18:18: error: invalid initialization of reference of type ‘Int&’ from expression of type ‘const Int’
for (Int& i : ints) {
^
Yes, I know I'm not actually trying to modify the elements in the for loop. But the point is that I should be able to get a non-const reference to use inside the loop, since the set itself is not const qualified. I get the same error if I create a setter function and use that in the loop.
A set is like a map with no values, only keys. Since those keys are used for a tree that accelerates operations on the set, they cannot change. Thus all elements must be const to keep the constraints of the underlying tree from being broken.
std::set uses the contained values to form a fast data structure (usually, a red-black tree). Changing a value means the whole structure needs to be altered. So, forcing constness, std::set prevents you from pushing it into a non-usable state.
From the cpp reference:
In a set, the value of an element also identifies it (the value is
itself the key, of type T), and each value must be unique. The value
of the elements in a set cannot be modified once in the container (the
elements are always const), but they can be inserted or removed from
the container.
The behaviour is by design.
Giving you a non-const iterator could inspire you to change the element in the set; the subsequent iterating behaviour would then be undefined.
Note that the C++ standard says that set<T>::iterator is const so the old-fashioned pre C++11 way still wouldn't work.
Adding on nate's answer:
A set is like a map with no values, only keys. Since those keys are used for a tree that accelerates operations on the set, they cannot change. Thus all elements must be const to keep the constraints of the underlying tree from being broken.
With C++17 there is the new extract member function, so an alternative to const_cast could be:
#include <iostream>
#include <string_view>
#include <set>
struct S
{
int used_for_sorting;
bool not_used_for_sorting;
bool operator<(const S &rhs) const
{ return used_for_sorting < rhs.used_for_sorting; }
};
void print(std::string_view comment, const std::set<S> &data)
{
std::cout << comment;
for (auto datum : data)
std::cout << " {" << datum.used_for_sorting
<< ',' << datum.not_used_for_sorting
<< '}';
std::cout << '\n';
}
int main()
{
std::set<S> cont = {{1, false}, {2, true}, {3, false}};
print("Start:", cont);
// Extract node handle and change key
auto nh = cont.extract({1, false});
nh.value().not_used_for_sorting = true;
print("After extract and before insert:", cont);
// Insert node handle back
cont.insert(std::move(nh));
print("End:", cont);
}
Probably useful as hot-fix. In general it's hard to see any advantage over a std::map.

Strange PC-Lint errors with member templates

I am currently struggling with PC-Lint (version 9.00j and l), which gives me some errors and warnings for a piece of code. The code compiles well and runs as expected. Here is a simplified version of it:
#include <iostream>
#include <vector>
typedef unsigned char uint8_t;
class Test
{
uint8_t inputList[10];
std::vector<int> resultList;
public:
Test() : resultList()
{
for (uint8_t ii = 0; ii < 10; ++ii)
inputList[ii] = ii;
}
template<int list_size, typename ResultListType>
void loadList(const uint8_t (& inputList)[list_size],
ResultListType & resultList) const
{
for (uint8_t ii = 0; ii < list_size; ++ii)
resultList.push_back(inputList[ii]);
}
void run()
{
loadList(inputList, resultList);
}
void print()
{
std::vector<int>::iterator it;
for (it = resultList.begin(); it != resultList.end(); ++it)
std::cout << *it << std::endl;
}
};
int main()
{
Test t;
t.run();
t.print();
}
When running this in Gimpel's online demo, I get these errors and warnings:
30 loadList(inputList, resultList);
diy.cpp 30 Error 1025: No template matches invocation 'Test::loadList(unsigned char [10], std::vector<int>)', 1 candidates found, 1 matched the argument count
diy.cpp 30 Info 1703: Function 'Test::loadList(const unsigned char (&)[V], <2>&) const' arbitrarily selected. Refer to Error 1025
diy.cpp 30 Error 1032: Member 'loadList' cannot be called without object
diy.cpp 30 Error 1058: While calling 'Test::loadList(const unsigned char (&)[V], <2>&) const': Initializing a non-const reference '<2>&' with a non-lvalue (a temporary object of type 'std::vector<int>')
diy.cpp 30 Warning 1514: Creating temporary to copy 'std::vector<int>' to '<2>&' (context: arg. no. 2)
So basically, PC-Lint is trying to tell me that it will just find the right template parameters by chance and that only a temporary copy of the vector will be filled. But the code runs well, the resultList contains the data!
Can anyone tell me what is going on here? Is PC-Lint right and something is going wrong or is this just a PC-Lint bug?
The problem is that loadList is marked to be const, and yet you pass a non-constant reference to the member variable resultList that you modify.
It's true that the loadList function doesn't modify this instance directly but since you still modify a member variable the function can't be constant.
Either create a temporary vector that you pass to the function, or make the function not const.

Getting information out of a set<MyClass> mySet

So I've got
set<MyClass> mySet;
and in MyClass I have a static int to count the number of compares that happen. I'm trying to get that information out of it but I can't figure out how.
This is what I've tried:
set<MyClass>::iterator it = mySet.begin();
int count = it->getCompareCount();
and
int count = mySet.begin()->getCompareCount();
Neither of those work (Yes I know the are essentially the same exact thing) but I can get that information out of something that is indexed like a std::list or std::vector
Example:
vector<MyClass> myVector;
for (int i = 0; i < 10; i ++)
{
myVector.push_back(MyClass(i,"Some Name", i*2);
}
int count = myVector.at(2).getCompareCount(); //which by default is going to be 0 as no compares have taken place
Can anybody help me? netbeans says "error: passing ‘const MyClass’ as ‘this’ argument of ‘int MyClass::getCompareCount()’ discards qualifiers [-fpermissive]" when I do what I said I tried up above.
EDITS
Declaration of getCompareCount():
int MyClass::getCompareCount()
{
return compareCount;
}
Element in std::set is not mutable after it is inserted into the set. When you dereference an iterator (as in it->), it returns a const reference of the object you put into the set. On the other hand, your definition of getCompareCount() is a non-const instance function meaning it can only be called on non-const instance of the class. Define the function as the following should solve your problem.
int MyClass::getCompareCount() const
{
return compareCount;
}
By the way, since compareCount is a static int, you might want to define the function as static as well so that you can call it using the class instead of having to get an instance to call it.
static int MyClass::getCompareCount()
{
return compareCount;
}
//To Call it
MyClass::getCompareCount()

Changing a classes member through an iterator

I'm learning C++ and can't get my head around this problem:
I have a simple class A
class A {
private:
int ival;
float fval;
public:
A(int i = 0, float f = 0.0) : ival(i), fval(f) { }
~A(){ }
void show() const {
cout << ival << " : " << fval << "\n";
}
void setVal(int i) {
ival = i;
}
//const getters for both ival and fval
//used for the default "lesser"
friend bool operator<(const A& val1, const A& val2) {
return val1.ival < val2.ival ? true : false;;
}
}
Then I have a regular set<A> myset that gets filled with insert(A(2, 2.2)); in a loop.
Iterating to get all the values is not a problem but I want to modify the value within this iteration:
for(set<A>::iterator iter = set3.begin(); iter != set3.end(); iter++) {
iter->setVal(1);
}
I assume that this should be doable, like you would do it in Java within a foreach loop. When compiling I get error: passing ‘const A’ as ‘this’ argument of ‘void A::setVal(int)’ discards qualifiers.
Looking at the sources of the STL set, i see that begin() is only available as a const method and I think this might be the problem. Messing around with const on the setVal() method got always the same error and wouldn't make much sense since I want to modify the value of A.
Is this the wrong approach of changing a bunch of A's values with a loop?
The STL set does not let you change values it stores. It does that by returning a copy of the object through the iterator (not the actual one in the set).
The reason that set does this is because it's using < to order the set and it doesn't want to remake the entire tree every time you dereference the iterator, which it would have to do, since it doesn't know if you changed anything that changes the ordering.
If you need to update the set<>, remove the old value and add in a new one.
EDIT: just checked source to SGI STL and it says this:
typedef typename _Rep_type::const_iterator iterator;
So, a set::iterator is just a set::const_iterator
From this page, it seems that begin() exists as well as a non-const method.
Perhaps your set is passed into the method as a const reference ?
EDIT
The referenced page is wrong. As Scharron states, there is no non-const begin() (or end() for that matter) method for ordered containers.
I will inform the website about their mistake (it's not the first they made ;))