Note that there are some explanatory texts on larger screens.

plurals
  1. POnon-resizeable vector/array of non-reassignable but mutable members?
    primarykey
    data
    text
    <p>Is there a way to make a non-resizeable vector/array of non-reassignable but mutable members? The closest thing I can imagine is using a <code>vector&lt;T *&gt; const</code> copy constructed from a temporary, but since I know at initialization how many of and exactly what I want, I'd much rather have a block of objects than pointers. Is anything like what is shown below possible with <code>std::vector</code> or some more obscure boost, etc., template?</p> <pre><code>// Struct making vec&lt;A&gt; that cannot be resized or have contents reassigned. struct B { vector&lt;A&gt; va_; // &lt;-- unknown modifiers or different template needed here vector&lt;A&gt; va2_; // All vector contents initialized on construction. Foo(size_t n_foo) : va_(n_foo), va2_(5) { } // Things I'd like allowed: altering contents, const_iterator and read access. good_actions(size_t idx, int val) { va_[idx].set(val); cout &lt;&lt; "vector&lt;A&gt; info - " &lt;&lt; " size: " &lt;&lt; va_.size() &lt;&lt; ", max: " &lt;&lt; va_.max_size() &lt;&lt; ", capacity: " &lt;&lt; va_.capacity() &lt;&lt; ", empty?: " &lt;&lt; va_.empty() &lt;&lt; endl; if (!va_.empty()) { cout &lt;&lt; "First (old): " &lt;&lt; va_[0].get() &lt;&lt; ", resetting ..." &lt;&lt; endl; va_[0].set(0); } int max = 0; for (vector&lt;A&gt;::const_iterator i = va_.begin(); i != va_.end(); ++i) { int n = i-&gt;get(); if (n &gt; max) { max = n; } if (n &lt; 0) { i-&gt;set(0); } } cout &lt;&lt; "Max : " &lt;&lt; max &lt;&lt; "." &lt;&lt; endl; } // Everything here should fail at compile. bad_actions(size_t idx, int val) { va_[0] = va2_[0]; va_.at(1) = va2_.at(3); va_.swap(va2_); va_.erase(va_.begin()); va_.insert(va_.end(), va2_[0]); va_.resize(1); va_.clear(); // also: assign, reserve, push, pop, .. } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload