Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Types that you put in a standard container have to be copyable and assignable. The reason that <code>auto_ptr</code> causes so much trouble is precisely because it doesn't follow normal copy and assignment semantics. Naturally, anything that's <code>const</code> is not going to be assignable. So, you can't stick <code>const</code> anything in a standard container. And if the element isn't <code>const</code>, then you <em>are</em> going to be able to change it.</p> <p>The closest solution that I believe is possible would be to use an indirection of some kind. So, you could have a pointer to const or you could have an object which holds the value that you want but the value can't be changed within the object (like you'd get with <code>Integer</code> in Java).</p> <p>Having the element at a particular index be unchangeable goes against how the standard containers work. You might be able to construct your own which work that way, but the standard ones don't. And none which are based on arrays will work regardless unless you can manage to fit their initialization into the <code>{a, b, c}</code> initialization syntax since once an array of <code>const</code> has been created, you can't change it. So, a <code>vector</code> class isn't likely to work with const elements no matter what you do.</p> <p>Having <code>const</code> in a container without some sort of indirection just doesn't work very well. You're basically asking to make the entire container <code>const</code> - which you could do if you copy to it from an already initialized container, but you can't really have a container - certainly not a standard container - which contains constants without some sort of indirection.</p> <p><strong>EDIT</strong>: If what you're looking to do is to mostly leave a container unchanged but still be able to change it in certain places in the code, then using a const ref in most places and then giving the code that needs to be able to change the container direct access or a non-const ref would make that possible.</p> <p>So, use <code>const vector&lt;int&gt;&amp;</code> in most places, and then either <code>vector&lt;int&gt;&amp;</code> where you need to change the container, or give that portion of the code direct access to the container. That way, it's mostly unchangeable, but you can change it when you want to.</p> <p>On the other hand, if you want to be able to pretty much always be able to change what's in the container but not change specific elements, then I'd suggest putting a wrapper class around the container. In the case of <code>vector</code>, wrap it and make the subscript operator return a const ref instead of a non-const ref - either that or a copy. So, assuming that you created a templatized version, your subscript operator would look something like this:</p> <pre><code>const T&amp; operator[](size_t i) const { return _container[i]; } </code></pre> <p>That way, you can update the container itself, but you can't change it's individual elements. And as long as you declare all of the functions inline, it shouldn't be much of a performance hit (if any at all) to have the wrapper.</p>
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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