The following situation: I am supposed to implement an API, which is getting tuples, and has to store them. Some member of these values are const&, because I am not supposed to change them (& because they are big).
So, I store them in a vector. But I also want to remove them from my vector again. But this, does not work, because I get the following error.
/usr/include/c++/5/tuple:299:17: error: assignment of read-only location ‘std::_Tuple_impl<_Idx, _Head, _Tail ...>::_M_head<0ul, const int&, {double}>(((std::_Tuple_impl<0ul, const int&, double>)this))’
Here a simplified variation of what I am doing:
#include <vector>
#include <tuple>
#include <algorithm>
using xxx = std::tuple<const int&, double>;
//not allowed to modify integer
void storeTupleWithValue(std::vector<xxx>& values, const int& value){
values.push_back(std::tuple<const int&, double>(value, 3.0));
}
//not allowed to modify integer
void deleteStoredTupleWithValue(std::vector<xxx>& values, const int& value){
//find iterator of "value"
//...
auto toDelete = values.begin();
values.erase(toDelete);
}
const int& produceItem(int& s){
return s;
}
int main()
{
std::vector<xxx> values;
int s = 5;
auto apiItem = produceItem(s)
storeTupleWithValue(values, s);
deleteStoredTupleWithValue(values, s);
}
So, how do I best resolve this situation, respectively manage to delete my memorized tuples again.
Playground of the code with the error message: https://onlinegdb.com/ry2t_Q5zV