title: Simulate std::vector with mixed const and non-const elements
body: <p>I'd like to simulate a std::vector that has mixed const and non-const elements. More specifically, I want to have functions that operate on a vector and are allowed to see the entire vector but may only write to specific elements. The elements that can and cannot be written will be determined at runtime and may change during runtime.</p>
  
  <p>One solution is to create a container that holds an array of elements and an equal sized array of booleans. All non-const access would be through a function that checks against the boolean array if the write is valid and throws an exception otherwise. This has the downside of adding a conditional to every write.</p>
  
  <p>A second solution might be to have the same container but this time write access is done by passing an array editing function to a member function of the container. The container member function would let the array editing function go at the array and then check that it didn't write to the non-writable elements. This has the downside that the array editing function could be sneaky and pass around non-const pointers to the array elements, let the container function check that all is well, and then write to non-writable elements.</p>
  
  <p>The last issue seems difficult to solve. It seems like offering direct writable access ever means we have to assume direct writable access always.</p>
  
  <p>Are there better solutions?</p>
  
  <p>EDIT: Ben's comment has a good point I should have addressed in the question: why not a vector of const and a vector of non-const?</p>
  
  <p>The issue is that the scenario I have in mind is that we have elements that are conceptually part of one single array. Their placement in that array is meaningful. To use vectors of const and non-const requires mapping the single array that exist in concept to the two vectors that would implement it. Also, if the list of writable elements changes then the elements or pointers in the two vectors would need to be moved about.</p>