Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The initialization of the elements allocated is controlled by the Allocator template argument, if you need it customized, customize it. But remember that this can get easily wind-up in the realm of dirty hacking, so use with caution. For instance, here is a pretty dirty solution. It will avoid the initialization, but it most probably will be worse in performance, but for demonstration's sake (as people have said this is impossible!... impossible is not in a C++ programmer's vocabulary!): </p> <pre><code>template &lt;typename T&gt; class switch_init_allocator : public std::allocator&lt; T &gt; { private: bool* should_init; public: template &lt;typename U&gt; struct rebind { typedef switch_init_allocator&lt;U&gt; other; }; //provide the required no-throw constructors / destructors: switch_init_allocator(bool* aShouldInit = NULL) throw() : std::allocator&lt;T&gt;(), should_init(aShouldInit) { }; switch_init_allocator(const switch_init_allocator&lt;T&gt;&amp; rhs) throw() : std::allocator&lt;T&gt;(rhs), should_init(rhs.should_init) { }; template &lt;typename U&gt; switch_init_allocator(const switch_init_allocator&lt;U&gt;&amp; rhs, bool* aShouldInit = NULL) throw() : std::allocator&lt;T&gt;(rhs), should_init(aShouldInit) { }; ~switch_init_allocator() throw() { }; //import the required typedefs: typedef typename std::allocator&lt;T&gt;::value_type value_type; typedef typename std::allocator&lt;T&gt;::pointer pointer; typedef typename std::allocator&lt;T&gt;::reference reference; typedef typename std::allocator&lt;T&gt;::const_pointer const_pointer; typedef typename std::allocator&lt;T&gt;::const_reference const_reference; typedef typename std::allocator&lt;T&gt;::size_type size_type; typedef typename std::allocator&lt;T&gt;::difference_type difference_type; //redefine the construct function (hiding the base-class version): void construct( pointer p, const_reference cr) { if((should_init) &amp;&amp; (*should_init)) new ((void*)p) T ( cr ); //else, do nothing. }; }; template &lt;typename T&gt; class my_vector : public std::vector&lt;T, switch_init_allocator&lt;T&gt; &gt; { public: typedef std::vector&lt;T, switch_init_allocator&lt;T&gt; &gt; base_type; typedef switch_init_allocator&lt;T&gt; allocator_type; typedef std::vector&lt;T, allocator_type &gt; vector_type; typedef typename base_type::size_type size_type; private: bool switch_flag; //the order here is very important!! vector_type vec; public: my_vector(size_type aCount) : switch_flag(false), vec(aCount, allocator_type(&amp;switch_flag)) { }; //... and the rest of this wrapper class... vector_type&amp; get_vector() { return vec; }; const vector_type&amp; get_vector() const { return vec; }; void set_switch(bool value) { switch_flag = value; }; }; class xyz{}; int main(){ my_vector&lt;xyz&gt; v(1024); //this won't initialize the memory at all. v.set_switch(true); //set back to true to turn initialization back on (needed for resizing and such) } </code></pre> <p>Of course, the above is awkward and not recommended, and certainly won't be any better than actually letting the memory get filled with copies of the first element (especially since the use of this flag-checking will impede on each element-construction). But it is an avenue to explore when looking to optimize the allocation and initialization of elements in an STL container, so I wanted to show it. The point is that the only place you can inject code that will stop the std::vector container from calling the copy-constructor to initialize your elements is in the construct function of the vector's allocator object.</p> <p>Also, you could do away with the "switch" and simply do a "no-init-allocator", but then, you also turn off copy-construction which is needed to copy the data during resizing (which would make this vector class much less useful). </p>
 

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