Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes std::vector use the assignment operator of its value type to push_back elements?
    primarykey
    data
    text
    <p>If so, why? Why doesn't it use the copy constructor of the value type?</p> <p>I get the following error:</p> <pre><code>/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc: In member functio n `ClassWithoutAss&amp; ClassWithoutAss::operator=(const ClassWithoutAss&amp;)': /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238: instantiate d from `void std::vector&lt;_Tp, _Alloc&gt;::_M_insert_aux(__gnu_cxx::__normal_iterato r&lt;typename _Alloc::pointer, std::vector&lt;_Tp, _Alloc&gt; &gt;, const _Tp&amp;) [with _Tp = ClassWithoutAss, _Alloc = std::allocator&lt;ClassWithoutAss&gt;]' /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_vector.h:564: instantia ted from `void std::vector&lt;_Tp, _Alloc&gt;::push_back(const _Tp&amp;) [with _Tp = Class WithoutAss, _Alloc = std::allocator&lt;ClassWithoutAss&gt;]' main.cpp:13: instantiated from here /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238: error: non-st atic const member `const int ClassWithoutAss::mem', can't use default assignment operator </code></pre> <p>running g++ main.cpp on the following code:</p> <pre><code>/* * ClassWithoutAss.h * */ #ifndef CLASSWITHOUTASS_H_ #define CLASSWITHOUTASS_H_ class ClassWithoutAss { public: const int mem; ClassWithoutAss(int mem):mem(mem){} ClassWithoutAss(const ClassWithoutAss&amp; tobeCopied):mem(tobeCopied.mem){} ~ClassWithoutAss(){} }; #endif /* CLASSWITHOUTASS_H_ */ /* * main.cpp * */ #include "ClassWithoutAss.h" #include &lt;vector&gt; int main() { std::vector&lt;ClassWithoutAss&gt; vec; ClassWithoutAss classWithoutAss(1); (vec.push_back)(classWithoutAss); return 0; } </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